无法将图像文件从临时文件移动到服务器上的永久文件夹

时间:2017-11-19 22:38:29

标签: php file-upload image-uploading

将html表单中的信息上传到我的数据库中没有问题,但我无法显示图像。我认为这与我无法将图像文件从临时文件正确地移动到服务器上的永久文件这一事实有关(这意味着我的$ imgURL变量持有错误的信息,我猜?)但是我无法理解。

更新:我现在可以将图像上传到临时位置(参见下面的picture.js)但由于某种原因,图像不会传递到最终目的地。当我尝试将图像从临时文件发送到其最终目标文件夹时,我收到“无文件”错误代码。关于为什么会这样的任何想法?

谢谢!

这是我的upload_func.php文件:

<?php

    $upload_errors=array();

    function file_upload($fileKey, $uploadDir){

        global $upload_errors;

        $error_code=null;
        $desc=null;
        $imgURL=null;
        $info=array();

        if(($_FILES[$fileKey]["type"]== "image/gif")||($_FILES[$fileKey]["type"]=="image/jpeg")
        || ($_FILES[$fileKey]["type"]== "image/jpg")|| ($_FILES[$fileKey]["type"]== "image/pjpeg")
        || ($_FILES[$fileKey]["type"]== "image/x-png")||($_FILES[$fileKey]["type"]== "image/png")
        && ($_FILES[$fileKey]["size"]< 3000000)){

            if($_FILES[$fileKey]["error"]>0){
                $error_code=$_FILES[$fileKey]["error"];
                $desc=$upload_errors[$error_code];
            }
            else{

                $uploaded_file = $_FILES[$fileKey]["name"];
                $tmp_file=$_FILES[$fileKey]["tmp_name"];
                $imgURL = $uploadDir.basename($uploaded_file); //http://129.2.24.54/ckmayo/geog650/lab3/uploads/1.jpg

                if(file_exists($uploaded_file)){
                    $error_code = -2;
                    $desc = "File already exists";
                }
                else{

                    if (move_uploaded_file($tmp_file, $imgURL)){
                        $error_code = 0;
                        $desc = "File uploaded successfully";
                    }
                    else{
                        $error_code = $_FILES[$fileKey]['error'];
                        $desc = $upload_errors[$error_code];
                    }
                }           
            }
        }
        else{
            if($_FILES[$fileKey]['error']>0){
                $error_code=$_FILES[$fileKey]['error'];
                $desc=$upload_errors[$error_code]; //$upload_errors is a global variable
            }
            else{
                $error_code = -1;
                $desc = "File upload error: Invalid file";
            }
        }


    $info["code"] = $error_code;
    $info["desc"] = $desc;
    $info["imgURL"] = $imgURL;
    return $info; 
    }   
?>

这是我的picture.js文件,我将照片发送到临时位置:

var imgURI;
var serverURL="http://geogthemis001.umd.edu/elim/geog650/lecture4/upload.php";

$(document).ready(function(){
    //attach a event handler function to the deviceready event using document.addEventListener()
    document.addEventListener("deviceready", getCameraReady, false);
});

//function onDeviceReady(){
    //getCameraReady(); 
//}

function photoSuccess(uri){
    $("#img").attr("src", uri);
    $("#img").css("width", "100%");
    $("#img").css("height", "100%");

    imgURI = uri;
}

function photoFail(message){
    alert("ERROR: "+ message);
}

function uploadSuccess(result){
    alert("File Upload Done");
    navigator.camera.cleanup();
    alert("Sent: "+ result.bytesSent + " \nResponse: "+ result.response);

}

function uploadFail(error){
    alert("An error has occured: Code " + error.code);
}

function getCameraReady(){
    $('#btCamera').on('click',function(e){

        options={quality: 50, destinationType: Camera.DestinationType.FILE_URI, sourceType: Camera.PictureSourceType.CAMERA};       
        navigator.camera.getPicture(photoSuccess, photoFail, options);

    });

    $('#btPhotoLib').on('click',function(e){

        options={quality: 50, destinationType: Camera.DestinationType.FILE_URI, sourceType: Camera.PictureSourceType.PHOTOLIBRARY};     
        navigator.camera.getPicture(photoSuccess, photoFail, options);

    });

    $('#btUpload').on('click',function(e){
        var options = new FileUploadOptions();
        options.fileKey = "file";
        options.fileName = imgURI.substr(imgURI.lastIndexOf('/')+1);
        options.mimeType = "image/jpeg"; //how to get the mime type of data

        var ft = new FileTransfer();
        var server = encodeURI(serverURL);
        //alert(imgURI);
        ft.upload(imgURI, server, uploadSuccess, uploadFail, options);

    });
}   

1 个答案:

答案 0 :(得分:0)

我想问题是您尝试将图片移动到网址并且没有任何意义......

您是否尝试通过$imgURL替换(在move_uploaded_file调用中)变量$uploaded_file

你应该尝试:

if (move_uploaded_file($tmp_file, $uploaded_file)){