Jump to content

File Upload Script


[dx]
 Share

Recommended Posts

hi, I have those 2 scripts:// file upload.php

<?PHPecho '<title>File upload</title>';if (!isset($_COOKIE['cookiename'])) { exit("<p style=\"padding: 50px 0px; font-weight: bold; text-align: center; width: 100%\">Access denied!</p>"); }echo '<table style="width: 100%; height: 480px; padding: 0px 20px;"><tr><td>	<form enctype="multipart/form-data" action="uploadp.php" method="post" />	<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />	<input type="file" name="datafile" size="50"/><br /><br />	<input type="submit" value="Send" />	</form></td></tr></table>';?>

And // file uploadp.php

<?PHPecho '<title>File upload</title>';$fajl = $_FILES['datafile']['name'];$fajl_tmp = $_FILES['datafile']['tmp_name'];$target = "upload_folder";$target = "$target/$fajl";if (move_uploaded_file($fajl_tmp, $target)) {	echo 'File is successfully uploaded :)';}else {	echo 'Error during upload, try again :(';	header("location: upload.php");}?>

And works fine, fine uploads file and moves it in upload_folder directory.But sometimes it fails upload, and it just do back to upload.php again. I guess it process with this header when file is not moved. And what I'm wondering is why it fails. I found this in php error log:PHP Warning: POST Content-Length of 33482222 bytes exceeds the limit of 8388608 bytes in Unknown on line 0That's OK, becouse file is about 33 MB, but can we increase that number in wamp server. And why if fails with some other files which are below 8 MB and no errors for it?

Edited by Haris S
Link to comment
Share on other sites

Your upload script isn't doing any error checking at all. Look here for information about file upload error codes:http://www.php.net/manual/en/features.file-upload.errors.phpOther than that, you can find the various options in php.ini, you can set the max post size there. By default it's 8M.

Link to comment
Share on other sites

Your upload script isn't doing any error checking at all.
It's Wamp server's error chechker, and now it's OK, found other directive in php.ini; Maximum allowed size for uploaded files.upload_max_filesize = 2MGot if from error Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini. Edited by Haris S
Link to comment
Share on other sites

It's Wamp server's error chechker
No, upload errors don't get printed automatically, it just stores the error code in the $_FILES array and it's up to you to check what the error code is. Look at the link I posted. Here's a function I wrote for handling file uploads if you want to use it, it will return an error message if necessary:
function handle_file_upload($options){  $input = !empty($options['input']) ? $options['input'] : trigger_error('handle_file_upload: No input name specified', E_USER_ERROR);  $dest = !empty($options['dest_dir']) ? $options['dest_dir'] : trigger_error('handle_file_upload: No destination name specified', E_USER_ERROR);  $dest_fname = !empty($options['dest_fname']) ? $options['dest_fname'] : '';  $overwrite = !empty($options['overwrite']) ? $options['overwrite'] : false;  $mode = !empty($options['mode']) ? $options['mode'] : false;  $allowed_ext = isset($options['allowed_ext']) && is_array($options['allowed_ext']) ? $options['allowed_ext'] : false;  if (!is_dir($dest))	trigger_error('handle_file_upload: Destination directory does not exist', E_USER_ERROR);  if (!isset($_FILES[$input]))	trigger_error('handle_file_upload: The input file was not found', E_USER_ERROR);  if ($_FILES[$input]['error'] > 0)  {	switch ($_FILES[$input]['error'])	{	  case 1:	  case 2; return array('success' => false, 'error' => 'The uploaded file was too large.');	  case 3: return array('success' => false, 'error' => 'The uploaded file was only partially received.');	  case 4: return array('success' => false, 'error' => 'No file was uploaded.');	  case 6: return array('success' => false, 'error' => 'Missing temporary folder.');	  case 7: return array('success' => false, 'error' => 'Failed to write file to disk.');	  case 8: return array('success' => false, 'error' => 'Invalid extension.');	}  }    if ($allowed_ext != false && !in_array(strtolower(array_pop(explode('.', $_FILES[$input]['name']))), $allowed_ext))	return array('success' => false, 'error' => 'That file type was not allowed.');  if ($dest_fname != '')	$_FILES[$input]['name'] = $dest_fname;  $_FILES[$input]['name'] = strtolower(basename($_FILES[$input]['name']));  if (!$overwrite)  {	$fname = $_FILES[$input]['name'];	if (file_exists($dest . DIRECTORY_SEPARATOR . $fname))	{	  $chunks = explode('.', $fname);	  $ext = array_pop($chunks);	  $fname = implode('.', $chunks);	  $nr = 1;	  while (file_exists($dest . DIRECTORY_SEPARATOR . $fname . '.' . $ext))		$fname = $fname . '.' . $nr++;	  $_FILES[$input]['name'] = $fname . '.' . $ext;	}  }  $target = $dest . DIRECTORY_SEPARATOR . $_FILES[$input]['name'];  if (!move_uploaded_file($_FILES[$input]['tmp_name'], $target))	return array('success' => false, 'error' => 'The uploaded file could not be moved.');  if ($mode !== false)	chmod($target, $mode);  return array('success' => true, 'name' => $_FILES[$input]['name']);}

e.g.:

$file = handle_file_upload(array(  'input' => 'datafile',  'dest_dir' => 'path/to/upload_dir',  'overwrite' => false,  'mode' => 0644,  'allowed_ext' => array('jpg','jpeg','png','gif')));if (!$file['success']){  echo 'Upload error: ' . $file['error'];}else{  echo 'Uploaded ' . $file['name'];}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...