Thread: Php file upload
View Single Post
Join Date: Sep 2008
Posts: 130
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