PHP移动上传的文件返回false,但没关系

时间:2015-10-12 08:33:58

标签: php file-upload

我正在从我的Android应用程序发送图片到具有此php代码的服务器:

<?php

$file_path = "uploads/";

$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
    echo "success";
} else{
    echo "fail";
}
?>

上传完成后,图片位于预期文件夹中,但回显“失败”。知道正确的回声很重要,为什么回声可能会失败?

3 个答案:

答案 0 :(得分:0)

试试这段代码:

$file = $_FILES['fileup']['name'];
$tmp_name = $_FILES['fileup']['tmp_name'];
$target_dir = "uploads/".basename($file);
if($file!="")
{  
    if(move_uploaded_file($tmp_name,$target_dir))
    {
        $fileErr = "Image Upload Success";
    }
    else
    {
        $filer = "Image Upload Not Success";
    }
}

答案 1 :(得分:0)

您可以尝试使用错误返回值来显示错误类型,如:

<?php
$errors=array(
    UPLOAD_ERR_INI_SIZE     =>  'UPLOAD_ERR_INI_SIZE',
    UPLOAD_ERR_FORM_SIZE    =>  'UPLOAD_ERR_FORM_SIZE',
    UPLOAD_ERR_PARTIAL      =>  'UPLOAD_ERR_PARTIAL',
    UPLOAD_ERR_NO_FILE      =>  'UPLOAD_ERR_NO_FILE',
    UPLOAD_ERR_NO_TMP_DIR   =>  'UPLOAD_ERR_NO_TMP_DIR',
    UPLOAD_ERR_CANT_WRITE   =>  'UPLOAD_ERR_CANT_WRITE',
    UPLOAD_ERR_EXTENSION    =>  'UPLOAD_ERR_EXTENSION'
);

$file_path = "uploads/";

$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if( move_uploaded_file( $_FILES['uploaded_file']['tmp_name'], $file_path ) ) {
    echo "success";
} else{
    $error=$errors[ $_FILES["uploaded_file"]["error"] ];
    echo "fail:" . $error;
}
?>

答案 2 :(得分:0)

Android程序员必须使用

InputStream is = new BufferedInputStream(conn.getInputStream());
String s = convertInputStreamToString(is);

现在回归成功。 convertInputStreamToString是我自己的方法,但每个人都可以想出来写它。 @mcklayin的建议是正确的。