Php file upload

Reply

Join Date: Sep 2008
Posts: 132
Reputation: rajeesh_rsn is an unknown quantity at this point 
Solved Threads: 0
rajeesh_rsn rajeesh_rsn is offline Offline
Junior Poster

Php file upload

 
0
  #1
Jan 8th, 2009
Hi I am new to php,
I had a form in my page to upload image and some data. And that image filed is not mandatory. I had done that script well and working well. But the problem is it always need a image ( that is not mandatory in my case ). The following is the code with me.. Kindly let me know which change i need to do for making that file field is not mandatory ...

  1. $string = md5(microtime() * mktime());
  2. $new = substr($type,0,1);
  3. $ido = substr($string,0,6);
  4. $id= $new.$ido;
  5.  
  6.  
  7. $directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
  8.  
  9. // make a note of the directory that will recieve the uploaded files
  10. $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . "shopping/";
  11.  
  12. // make a note of the location of the upload form in case we need it
  13. $uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'shopping.php';
  14.  
  15. // make a note of the location of the success page
  16. $uploadSuccess ='shopping.php';
  17.  
  18. // name of the fieldname used for the file in the HTML form
  19. $fieldname = 'file';
  20.  
  21.  
  22. $errors = array(1 => 'php.ini max file size exceeded',
  23. 2 => 'html form max file size exceeded',
  24. 3 => 'file upload was only partial',
  25. 4 => 'no file was attached');
  26.  
  27. // check the upload form was actually submitted else print form
  28. isset($_POST['Submit'])
  29. or error('the upload form is neaded', $uploadForm);
  30.  
  31. // check for standard uploading errors
  32. ($_FILES[$fieldname]['error'] == 0)
  33. or error($errors[$_FILES[$fieldname]['error']], $uploadForm);
  34.  
  35. // check that the file we are working on really was an HTTP upload
  36. @is_uploaded_file($_FILES[$fieldname]['tmp_name'])
  37. or error('not an HTTP upload', $uploadForm);
  38.  
  39. // validation... since this is an image upload script we
  40. // should run a check to make sure the upload is an image
  41. @getimagesize($_FILES[$fieldname]['tmp_name'])
  42. or error('only image uploads are allowed', $uploadForm);
  43.  
  44. // make a unique filename for the uploaded file and check it is
  45. // not taken... if it is keep trying until we find a vacant one
  46. $md5 = md5(microtime() * mktime());
  47.  
  48.  
  49. $string = substr($md5,0,5);
  50.  
  51. while(file_exists($uploadFilename = $uploadsDirectory.$string.'-'.$_FILES[$fieldname]['name']))
  52. {
  53. $string++;
  54. }
  55. $name = $string.'-'.$_FILES[$fieldname]['name'];
  56.  
  57.  
  58. // now let's move the file to its final and allocate it with the new filename
  59. @move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
  60. or error('receiving directory insuffiecient permission', $uploadForm);
  61.  
  62. rename("shopping/" . $name, "shopping/" .$type."-".$name);
  63.  
  64.  
  65. function error($error, $location, $seconds = 5)
  66. {
  67. header("Refresh: $seconds; URL=\"$location\"");
  68. echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n".
  69. '"http://www.w3.org/TR/html4/strict.dtd">'."\n\n".
  70. '<html lang="en">'."\n".
  71. ' <head>'."\n".
  72. ' <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n".
  73. ' <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n".
  74. ' <title>Upload error</title>'."\n\n".
  75. ' </head>'."\n\n".
  76. ' <body>'."\n\n".
  77. ' <div id="Upload">'."\n\n".
  78. ' <h1>Upload failure</h1>'."\n\n".
  79. ' <p>An error has occured: '."\n\n".
  80. ' <span class="red">' . $error . '...</span>'."\n\n".
  81. ' The upload form is reloading</p>'."\n\n".
  82. ' </div>'."\n\n".
  83. '</html>';
  84. exit;
  85. }

Thanks in Advance
Last edited by rajeesh_rsn; Jan 8th, 2009 at 1:39 pm. Reason: mistake
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 132
Reputation: rajeesh_rsn is an unknown quantity at this point 
Solved Threads: 0
rajeesh_rsn rajeesh_rsn is offline Offline
Junior Poster

Re: Php file upload

 
0
  #2
Jan 9th, 2009
Please help me....
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 55
Reputation: wilch is an unknown quantity at this point 
Solved Threads: 9
wilch wilch is offline Offline
Junior Poster in Training

Re: Php file upload

 
0
  #3
Jan 9th, 2009
What i would first attempt to do is to check whether the field 'file' has anything after being posted, if it is set to something (hopefully a filename) then we can proceed to call the other image processing stuff.
Otherwise we omit the image processing code.

i.e.


  1. //put the isset(...) stuff here
  2.  
  3. if ($_FILES['file']['name'] != '') { //can check for other things
  4.  
  5. // check for standard uploading errors
  6. ($_FILES[$fieldname]['error'] == 0)
  7. or error($errors[$_FILES[$fieldname]['error']], $uploadForm);
  8.  
  9. // check that the file we are working on really was an HTTP upload
  10. @is_uploaded_file($_FILES[$fieldname]['tmp_name'])
  11. or error('not an HTTP upload', $uploadForm);
  12.  
  13. // validation... since this is an image upload script we
  14. // should run a check to make sure the upload is an image
  15. @getimagesize($_FILES[$fieldname]['tmp_name'])
  16. or error('only image uploads are allowed', $uploadForm);
  17.  
  18. // make a unique filename for the uploaded file and check it is
  19. // not taken... if it is keep trying until we find a vacant one
  20. $md5 = md5(microtime() * mktime());
  21.  
  22.  
  23. $string = substr($md5,0,5);
  24.  
  25. while(file_exists($uploadFilename = $uploadsDirectory.$string.'-'.$_FILES[$fieldname]['name']))
  26. {
  27. $string++;
  28. }
  29. $name = $string.'-'.$_FILES[$fieldname]['name'];
  30.  
  31.  
  32. // now let's move the file to its final and allocate it with the new filename
  33. @move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
  34. or error('receiving directory insuffiecient permission', $uploadForm);
  35.  
  36. rename("shopping/" . $name, "shopping/" .$type."-".$name);
  37.  
  38. }
  39. //put that function error function here
  40. //----------------
I have put the code which i think affects the image into the if clause. All code before and after should remain as before.
Last edited by peter_budo; Jan 11th, 2009 at 2:22 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
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