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