file upload code.....need attention

Reply

Join Date: Aug 2007
Posts: 64
Reputation: nil_gh_80 is an unknown quantity at this point 
Solved Threads: 0
nil_gh_80's Avatar
nil_gh_80 nil_gh_80 is offline Offline
Junior Poster in Training

file upload code.....need attention

 
0
  #1
Aug 31st, 2007
plz rectify the following code of file upload

file_up.php

  1. <html>
  2. <body>
  3. <form enctype="multipart/form-data" action="upload.php" method="post">
  4. <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
  5. Choose a file to upload: <input name="uploaded_file" type="file" />
  6. <input type="submit" value="Upload" />
  7. </form>
  8. </body>
  9. </html>
___________________________________________________________

upload.php

  1. <?php
  2. //Сheck that we have a file
  3. if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  4. //Check if the file is JPEG image and it's size is less than 350Kb
  5. $filename = basename($_FILES['uploaded_file']['name']);
  6. $ext = substr($filename, strrpos($filename, '.') + 1);
  7. if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") &&
  8. ($_FILES["uploaded_file"]["size"] < 350000)) {
  9. //Determine the path to which we want to save this file
  10. $newname = dirname(__FILE__).'/upload/'.$filename;
  11. //Check if the file with the same name is already exists on the server
  12. if (!file_exists($newname)) {
  13. //Attempt to move the uploaded file to it's new place
  14. if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
  15. echo "It's done! The file has been saved as: ".$newname;
  16. } else {
  17. echo "Error: A problem occurred during file upload!";
  18. }
  19. } else {
  20. echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
  21. }
  22. } else {
  23. echo "Error: Only .jpg images under 350Kb are accepted for upload";
  24. }
  25. } else {
  26. echo "Error: No file uploaded";
  27. }
  28. ?>

when i try to upload any .jpg file under 350kb the last second error occur....I need ur suggestion that to run these code perfectly........
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: file upload code.....need attention

 
0
  #2
Aug 31st, 2007
Well, you have three conditions there that could be causing it to fall through
  1. if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && ($_FILES["uploaded_file"]["size"] < 350000)) {
Have you checked them all individually? This is just part of basic debugging. Use echo, var_dump(), or assert() statments to check these expressions individually.

Example of using assert()
  1. <?php
  2. // Active assert and make it quiet
  3. assert_options(ASSERT_ACTIVE, 1);
  4. assert_options(ASSERT_WARNING, 0);
  5. assert_options(ASSERT_QUIET_EVAL, 1);
  6.  
  7. // Create a handler function
  8. function my_assert_handler($file, $line, $code)
  9. {
  10. echo "<hr>Assertion Failed:
  11. File '$file'<br />
  12. Line '$line'<br />
  13. Code '$code'<br /><hr />";
  14. }
  15.  
  16. // Set up the callback
  17. assert_options(ASSERT_CALLBACK, 'my_assert_handler');
  18.  
  19. $a=1;
  20. $b=2;
  21.  
  22. // Make an assertion that should fail
  23. assert ($a==$b);
  24.  
  25. ?>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC