Page 1 of 1

Trouble with move_uploaded_file()

Posted: Thu Aug 24, 2006 8:13 am
by sejf83
I am migrating an app to a new server and I'm having trouble getting the move_uploaded_file function to work on the new server.

I did a print_r($_FILES) and I know the file to be uploaded is valid and is in the max_upload_size limit. However, move_uploaded_file() returns false with no error message and I can't figure out why.

Here is a link to my phpinfo();

http://www.ucl.ac.uk/eastman/nutrident/check.php

And here is the pertinent code...

Code: Select all

$destdir = 'filestore/';

$fileNameArray = explode(".",$_FILES["userfile"]["name"]);
$fileBase = $fileNameArray[0];
$ext = $fileNameArray[1];
$destfile = $destdir.$fileBase.".".$ext;

switch($_FILES["userfile"]["error"])
   {
      case 0:
      if(move_uploaded_file($_FILES["userfile"]["tmp_name"], $destfile))
      {
         //Change the file permissions
         chmod($destfile, 0755);
      }
      else
        $_SESSION["uploadError"] = "<span>Upload error!</span>";
      break;
      
      case 1:        
         $_SESSION["uploadError"] = "<span>Max size exceeded. Please upload a smaller file.</span>";
	  break;
      case 2: 
      case 3:
         $_SESSION["uploadError"] = "<span>File Upload Incomplete! Please Try Again.</span>\n";
	  break;
      case 4:
         $_SESSION["uploadError"] = "<span>Please select a valid file.</span>\n";
	  break;
   }
$_SESSION["uploadError"] always returns "Upload Error!".

Is there something wrong with my php.ini?

Thanks.

Posted: Thu Aug 24, 2006 10:07 am
by feyd
Make sure $destfile is correct. Consider the following too: what if I named the file "foo.exe.jpg"? What about just "foo"?

If $destfile is correct, run the following in a new file and tell us the results please.

Code: Select all

<?php

$neg = array('off', 0, false, '', null);
$flags = array(
	'Register Globals' => 'register_globals',
	'Short Tags' => 'short_open_tag',
	'Display Errors' => 'display_errors',
	'Magic Quotes GPC' => 'magic_quotes_gpc',
	'Magic Quotes Runtime' => 'magic_quotes_runtime',
	'Magic Quotes Sybase' => 'magic_quotes_sybase',
);
$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
foreach ($flags as $n => $v)
{
	$flags[$n] = (in_array(strtolower(ini_get($v)), $neg) ? 'Off' : 'On');
}
$cli = (php_sapi_name() == 'cli');
$eol = "\n";

$gle = get_loaded_extensions();
$rows = array();
$le = '';
$wide = 4;
$j = count($gle);
$pad = $wide - $j % $wide;
$len = max(array_map('strlen', $gle));
$func = create_function('$a', 'return str_pad($a, ' . intval($len) . ');');
$gle = array_map($func, $gle);
for($i = 0; $i < $j; $i += $wide)
{
	$le .= '   ' . implode('   ', array_slice($gle, $i, $wide)) . $eol;
}

$ec = array(
	'E_STRICT' => 2048, 'E_ALL' => 2047, 'E_USER_NOTICE' => 1024,
	'E_USER_WARNING' => 512, 'E_USER_ERROR' => 256, 'E_COMPILE_WARNING' => 128,
	'E_COMPILE_ERROR' => 64, 'E_CORE_WARNING' => 32, 'E_CORE_ERROR' => 16,
	'E_NOTICE' => 8, 'E_PARSE' => 4, 'E_WARNING' => 2, 'E_ERROR' => 1,
);

$e = array();
$t = $er;
foreach ($ec as $n => $v)
{
	if (($t & $v) == $v)
	{
		$e[] = $n;
		$t ^= $v;
	}
}
if (ceil(count($ec) / 2) + 1 < count($e))
{
	$e2 = array();
	foreach ($ec as $n => $v)
	{
		if (!in_array($n, $e) and $n != 'E_ALL')
		{
			$e2[] = $n;
		}
	}
	$er = $er . ' ((E_ALL | E_STRICT) ^ ' . implode(' ^ ', $e2) . '))';
}
else
{
	$er = $er . ' (' . implode(' | ', $e) . ')';
}

if (!$cli)
{
	echo '<html><head><title>quick info</title></head><body><pre>', $eol;
}

echo 'PHP Version: ', $ve, $eol;
echo 'PHP OS: ', $os, $eol;
echo 'Error Reporting: ', $er, $eol;
foreach ($flags as $n => $v)
{
	echo $n, ': ', $v, $eol;
}
echo 'Loaded Extensions:', $eol, $le, $eol;

if (!$cli)
{
	echo '</pre></body></html>', $eol;
}

?>