文件上载已停止


File upload stopped working

我上传了这个工作文件,自从我更改了web主机后,它就停止了工作,尽管我没有更改任何内容。

我检查了是否允许上传文件,并且最大文件大小设置为256MB,所以问题不存在。此外,正在上传的目录将CHMOD设置为777。

PHP版本为5.6.5

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

这是我的客户代码:

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";
} 

正如您所看到的,我在targetpath上做了一个vardump,这表明文件已经上传到客户端临时文件夹中。我检查了文件夹,文件已按预期上传到该文件夹。

我的服务功能如下:

$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);
});

您应该再次注意到,在move_uploaded_FILES失败后,我在代码底部检查了$_FILES是否为null。签入浏览器调试器显示$_FILES为null。

$_FILES变量为什么设置为null。。在我更换网络主机之前,它运行得很顺利。。。

我认为可能存在错误的唯一地方是,我通过postfields数组将文件"中继"到服务,但我不确定:

$data = array('optional_id' => $optional_id, 'file' => '@'.$target_path, 'upload_type' => $upload_type);  

根据这一点,@filename在PHP中似乎已被弃用>=5.5.0:有人能给我一个PHP的例子吗;s CURLFile类?