dongxichan8627 2015-11-24 21:37
浏览 92
已采纳

文件上传已停止工作

I had this working file upload which has stopped working since i changed web host although i have not changed anything.

I Checked that I'm allowed to upload files and max file size is set to 256MB so the problem is not there. Also the directories im uploading to have CHMOD set to 777.

PHP version is 5.6.15

My client which uploads the file to my service resides on the same domain.

Heres is my client code:

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

as you can see i did a var_dump on my target_path, which shows that the file has been uploaded to the clients temporary folder. I check the folder and the file was uploaded to that folder as expected.

My service function looks like this:

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

});

Again you should notice that i did a check if $_FILES is null at the bottom of the code after the move_uploaded_files has failed. Checking in my browser debugger shows me that $_FILES is null.

How come the $_FILES variable be set to null ? .. it was running smoothly before i changed webhost...

Only place where i figured there could be an error is where i "relay" the file to the service through the postfields array but im not sure:

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

1条回答 默认 最新

  • douchuza8908 2015-11-25 09:25
    关注

    According to this, it appears that @filename has been deprecated in PHP >= 5.5.0: Can anyone give me an example for PHP's CURLFile class?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元