Jump to content

Uploading Files Problem


frankieJr

Recommended Posts

Hello,I have a form that allows users to upload images .. but i cant seem to get it to work.. i think it has something to do with permissions... here's the code im using:$target_path = "uploadz/";$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['new_pic']['tmp_name'], $target_path)) { $msg = "The file ". basename( $_FILES['new_pic']['name'])." has been uploaded";} else{ $msg = "There was an error uploading the file, please try again!";}<form method='POST' action=\"$_SERVER[php_SELF]\" enctype=\"multipart/form-data\" ><input type='file' name='new_pic' id='new_pic' size='46' /><input type='image' src='save.jpg' name='submit' value='Upload File' border='0' />i made a folder called 'uploadz' and set permissions to 777 .. but i dont know where is the temp folderplease help.

Link to comment
Share on other sites

You're not really doing any error checking there. Here's a function you can use to handle an uploaded file, you can look through it to see what all it does:

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 directory 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']);}

example:

$file = handle_file_upload(array(  'input' => 'new_pic',  'dest_dir' => dirname(__FILE__) . DIRECTORY_SEPARATOR . 'uploadz'));if ($file['success'])  echo $file['name'] . ' was uploaded';  else  echo $file['error'];

The input and dest_dir options are required, you can also use these:dest_fname: give the file a specific name to be saved as, else it uses the original nameoverwrite: true to overwrite an existing file with the same name, else it will rename the new file if one already existsmode: set the permissions on the new file after uploading, given as an octal mode (e.g. 0644)allowed_ext: array of extensions to allow, will return an error if the file was something elsee.g.:

$file = handle_file_upload(array(  'input' => 'new_pic',  'dest_dir' => dirname(__FILE__) . DIRECTORY_SEPARATOR . 'uploadz',  'dest_fname' => 'new_file.ext',  'overwrite' => true,  'mode' => 0755,  'allowed_ext' => array('jpg', 'jpeg', 'gif', 'png')));if ($file['success'])  echo $file['name'] . ' was uploaded';  else  echo $file['error'];

It returns an array containing a property called success, and based on that either a property called error, or a property called name with the new filename. The reason you would use that filename is in case the function needed to rename the file to avoid overwriting an existing file.

Link to comment
Share on other sites

  if (!isset($_FILES[$input]))	trigger_error('handle_file_upload: The input file was not found', E_USER_ERROR);

That means the input option was wrong. The input option should be the name of the file input.

Link to comment
Share on other sites

how can i fix it?i did the code exactly as u posted it:

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 directory 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']);}$file = handle_file_upload(array(  'input' => 'new_pic',  'dest_dir' => dirname(__FILE__) . DIRECTORY_SEPARATOR . 'uploadz',  'dest_fname' => 'new_file.ext',  'overwrite' => true,  'mode' => 0755,  'allowed_ext' => array('jpg', 'jpeg', 'gif', 'png')));if ($file['success'])  echo $file['name'] . ' was uploaded';  else  echo $file['error'];

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...