从iPhone上传到PHP的文件无效

时间:2011-09-17 10:01:15

标签: php iphone

从iPhone上传文件时,我的代码会产生以下错误:

  

警告:move_uploaded_file()无法在/ var / www / ds1134 / https中将'/ tmp / phpUcqFVq'移动到'/var/www/ds1134/http.www.xxx.com/app/1316254141147.jpg'。 www.xxx.com/user.php第2866行

$_FILES看起来像:

 [file] => Array
 (
    [name]     => 1316250632283.jpg
    [type]     => 
    [tmp_name] => /tmp/phpFio7gb
    [error]    => 0
    [size]     => 35515
 )

我上传的PHP代码是:

if (move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/ds1134/http.www.xxx.com/app/".$_FILES["file"]["name"]))
{
    echo "done";
}

1 个答案:

答案 0 :(得分:1)

您可以添加一些前置条件检查以找出无法写入文件的原因:

$tmpName = $_FILES["file"]["tmp_name"];
$destDir = "/var/www/ds1134/http.www.xxx.com/app";
$destName = $destDir.$_FILES["file"]["name"];

if (!is_directory($destDir)
{
    throw new Exception('Destination is not a directory.');
}

if (!is_writable($destDir))
{
    throw new Exception('Destination directory is not writable.');
}

if (!preg_match('/^\d{13}\.jpg$/', $destName)
{
    throw new Exception('Invalid destination filename given. Only accepting 13 digits plus ".jpg".');
}

$destination = $destDir.'/'.$destName;

if (is_file($destination))
{
    throw new Exception('Destination filename already exists.');
}

if (virus_found($tmpName))
{
    throw new Exception('Upload file contains a virus.');
}

if (!imagecheck_validate_jpg($tmpName, 'color,24bit,size<1mb,in300x300,browser-compatible'))
{
    throw new Exception('Uploaded file does not meet the image requirements.');
}

if (move_uploaded_file($tmpName, $destination))
{
    echo "done";
}
else
{
    throw new Exception('Unable to move file.');
}