文件上传已停止工作

时间:2015-11-24 21:38:00

标签: php file-upload

我有这个工作文件上传,因为我改变了网络主机已停止工作,虽然我没有改变任何东西。

我检查过我是否允许上传文件,最大文件大小设置为256MB,因此问题不存在。我上传的目录也将CHMOD设置为777。

PHP版本为5.6.15

将文件上传到我的服务的客户端位于同一个域中。

Heres是我的客户代码:

strcpy

正如您所看到的,我在target_path上执行了var_dump,这表明该文件已上传到客户端临时文件夹。我检查文件夹,文件按预期上传到该文件夹​​。

我的服务功能如下:

error_reporting(E_ALL);

$uploaddir = 'tempUploads/';
$random_file_name = uniqid(rand(), true);
$file_extension = pathinfo(basename($_FILES['fileToUpload']['name']), PATHINFO_EXTENSION);
$uploadfile = $uploaddir . basename($_FILES['fileToUpload']['name']);
$optional_id = $_POST['optional_id'];
$upload_type = $_POST['upload_type'];

if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadfile)) 
{   
    $ch = curl_init();

    $filepath = str_replace('\\', '/', realpath(dirname(__FILE__))) . "/";  
    $target_path = $filepath . $uploadfile; 
    var_dump(file_exists($target_path));
    $data = array('optional_id' => $optional_id, 'file' => '@'.$target_path, 'upload_type' => $upload_type);    

    curl_setopt($ch, CURLOPT_URL, 'http://domain.dk/webservice/v1/uploadFile');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $curl_response = curl_exec($ch);

    if ($curl_response === false) 
    {
        $info = curl_getinfo($curl);
        curl_close($curl);
        die('error occured during curl exec. Additioanl info: ' . var_export($info));
    }   
} 
else 
{
    echo "Kunne ikke uploade filen";
} 

再次你应该注意到我在move_uploaded_files失败后检查了代码底部的$ _FILES是否为空。检入我的浏览器调试器会显示$ _FILES为空。

为什么$ _FILES变量设置为null? ..在我改变webhost之前它运行得很顺利......

只有我认为可能存在错误的地方才是我和#34;继电器"通过postfields数组到服务的文件,但我不确定:

$app->post('/uploadFile', 'authenticate', function() use ($app) 
{   
$optional_id = $app->request->post('optional_id');
$upload_type = $app->request->post('upload_type');
$file = $app->request->post('file');
$uploaddir = "";
// set directory where file needs to be saved
switch($upload_type)
{
    case 1:
        $uploaddir = '../uploadedContent/calibrationCertificate/';
        break;
    case 2:
        $uploaddir = '../uploadedContent/educationCertificate/';
        break;
    default:            
}


// random file name to avoid overwriting existing files.
$random_file_name = uniqid(rand(), true);
// extension of the file
$file_extension = pathinfo(basename($_FILES['file']['name']), PATHINFO_EXTENSION);
// full path and name
$uploadfile = $uploaddir . $random_file_name.".".$file_extension;


if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
{
    $db = new DbHandler(); 

    switch($upload_type)
    {
        case 1:
            $db->insertEquipmentDocumentRef($optional_id,$uploadfile,basename($_FILES['file']['name']));    
            break;
        case 2:
            $db->insertEducationDocumentRef($optional_id,$uploadfile,basename($_FILES['file']['name']));
            break;
        default:            
    } 

    $response["error"] = false;
    $response["message"] = "filen blev uploadet";
    $response["optional_id"] = $optional_id;
    $response["upload_type"] = $upload_type;
    $response["uploadfile"] = $file;
}
else
{
    $response["error"] = false;
    $response["message"] = "filen kunne ikke uploades";
    $response["dir"] = $uploadfile;
    $response["dir"] = $uploadfile;
    $response["move from"] = pathinfo(basename($_FILES['file']['name']));
    if(!$_FILES)
    {
        $response["file"] = "File does not exist";
    }

}

echoRespnse(201, $response);

});

1 个答案:

答案 0 :(得分:1)

根据这一点,似乎在PHP> = 5.5.0中已弃用@filename: Can anyone give me an example for PHP's CURLFile class?