在php中检索上传图片

时间:2010-12-27 05:50:42

标签: php

我想从目标c中编程的客户端(ipod)中检索图像,我使用以下代码

$TARGET_PATH = "pics/";
$image = $_FILES['photo'];
$TARGET_PATH =$TARGET_PATH . basename( $_FILES['photo']['name']);
$TARGET_PATH =$TARGET_PATH.".jpg";
if(file_exists($TARGET_PATH))
    {
$TARGET_PATH =$TARGET_PATH .uniqid() . ".jpg";
}
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
{
    $TARGET_PATH="http://www.".$_SERVER["SERVER_NAME"]."/abc/".$TARGET_PATH;
        echo $TARGET_PATH;
        echo "image upload successfully";}
else{
    echo "could not upload image";
}

这段代码成功上传了五到六张图片,之后它给了我错误,即

Notice:  Undefined index:  photo in /home/abc/public_html/abc.com/fish/mycatch_post.php on line 42
Notice: Undefined index: photo in /home/abc/public_html/abc.com/fish/mycatch_post.php on line 53
could not upload image

1 个答案:

答案 0 :(得分:1)

错误消息似乎告诉您全局$_FILES数组中的键“photo”上没有此类元素。这意味着客户端甚至没有在“照片”字段中发送文件。

与任何其他输入参数一样,您应该在尝试访问元素之前验证此条件。例如,使用:if (isset($_FILES['photo'])) {...}。即使设置了$_FILES['photo'],您也应该$_FILES['photo']['error']检查UPLOAD_ERR_PARTIAL是否有部分上传(UPLOAD_ERR_NO_FILE)或空上传({{1}})等异常,如PHP Manual所述}。

您可以通过添加适当的验证来调试此问题,即确保文件上传存在且已成功。