Uploading Files Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
frankiejr
Forum Newbie
Posts: 6
Joined: Tue Jul 21, 2009 5:39 pm

Uploading Files Problem

Post by frankiejr »

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:

Code: Select all

$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!";
}
 

Code: Select all

<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 folder

please help.
Last edited by frankiejr on Tue Jul 21, 2009 7:39 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Uploading Files Problem

Post by requinix »

I don't see the part of your post where you explain why you think there's a problem.
frankiejr
Forum Newbie
Posts: 6
Joined: Tue Jul 21, 2009 5:39 pm

Re: Uploading Files Problem

Post by frankiejr »

sorry about that...

i just applied an error handling function to see where the problem is ..

Code: Select all

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'];


when uploading a file i get this error: The input file was not found

any idea what does than mean??
Last edited by frankiejr on Tue Jul 21, 2009 7:35 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Uploading Files Problem

Post by requinix »

Are you making sure that the code you posted only executes after the form is uploaded?

Also: when you post code, put it in [syntax=php][/syntax] tags.
frankiejr
Forum Newbie
Posts: 6
Joined: Tue Jul 21, 2009 5:39 pm

Re: Uploading Files Problem

Post by frankiejr »

yes .. im using :

Code: Select all

if (($_POST['new_pic'])) { 
 
...
 
}
User avatar
airy
Forum Newbie
Posts: 20
Joined: Fri Jul 11, 2008 2:53 am

Re: Uploading Files Problem

Post by airy »

Code: Select all

 
if ($_FILES['new_pic']['error'] == 0) {
 
}
 
the $_POST['new_pic'] is null if html code is like this

Code: Select all

 
<form encrypt="form/multi-form-data" method="post" action="">
  <input type="file" name="new_pic />
</form>
 
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Uploading Files Problem

Post by superdezign »

frankiejr wrote:but i dont know where is the temp folder
Echo the tmp_name if you're curious.

And the error you're getting now sounds like the form wasn't submitted. The $_FILES array shouldn't be empty after you submit the form.
frankiejr
Forum Newbie
Posts: 6
Joined: Tue Jul 21, 2009 5:39 pm

Re: Uploading Files Problem

Post by frankiejr »

the form is being submitted, i tried adding another input and the data was submitted with no problem .. its the file i cant get it to upload!
frankiejr
Forum Newbie
Posts: 6
Joined: Tue Jul 21, 2009 5:39 pm

Re: Uploading Files Problem

Post by frankiejr »

ok .. no idea wats wrong with this form... i changed it to action='uploader.php' but it doesnt go there.. the page stays at the same location # !!!!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Uploading Files Problem

Post by superdezign »

frankiejr wrote:ok .. no idea wats wrong with this form... i changed it to action='uploader.php' but it doesnt go there.. the page stays at the same location # !!!!
When you make a change, the change will be occur. If the change does not occur, the change was not made (or was overwritten). You'll make it easier for us to help you if you weren't vague and told us what you are doing, what you want it to do, and what it is actually doing.

Also, *debug*. Echo information at every step to pinpoint the problem. print_r() your $_POST and $_FILES arrays. Display everything to the screen that you *think* is working, and, if it isn't, fix it. Proper troubleshooting will always help.
Post Reply