2
我創建了一個php腳本來處理上傳,我試圖在出現錯誤時立即返回錯誤(例如文件已經存在)。如何在php中打斷curl上傳?
不幸的是,根據我的測試,雖然我立即返回,捲曲將繼續上傳整個文件,直到它結束:
public function uploadFile($filename) {
Log::info('Uploading file...' . $filename);
register_shutdown_function(array(&$this, "streamInterrupted"));
$this->bytes_transferred=0;
set_time_limit(0);
ignore_user_abort(false);
$path = Config::get('agent.files_root'). '/' . $filename;
if (file_exists($path)) {
/*
* yes, I know, this needs to be worked on
* as it would allow a malicious user to find out
* if a file exists on the server...
*/
Log::error('Oops, the file already exists! ' . $filename);
return Response::json(array(
'error' => true,
'message' => 'That file already exists.'),
400
);
}
$putdata = fopen("php://input", "r");
/* Open a file for writing */
$fp = fopen($path, "w");
/* Read the data 1 KB at a time
and write to the file */
while ($data = fread($putdata, 1024))
fwrite($fp, $data);
/* Close the streams */
fclose($fp);
fclose($putdata);
Log::info('Yay! The file was uploaded successfully! ' . $filename);
return Response::json(array(
'error' => false,
'message' => 'The file was uploaded successfully!'),
400
);
}
所以,你可以看到,之前我甚至開始讀取PUT數據,我驗證文件是否存在並立即返回一個錯誤(順便說一下,我可以看到測試正在成功觸發),但是直到它完成所有數據的推送,curl纔會放棄。
有沒有辦法讓我乾淨地打斷它?我相當肯定我可以死(但)然後我不能將狀態碼和錯誤消息推回客戶端...
有什麼想法?
謝謝 路易斯