Saturday 3 December 2016

php - The second argument to copy() function cannot be a directory in move_uploaded_file



Im studying how to upload PDF with php and Im having a problem that Im not able to find what I have wrong.



I have an input type = "file" like this:







And then I have the code below, to create a directory to store pdfs, if the directory didnt exist yet.



if(!empty($_FILES['pdf']['tmp_name'])){


$folder = '../uploads/pdfs';
$year = date('Y');
$month = date('m');

if(!file_exists($folder.$year)){
mkdir($folder.$month,0755);

}
if(!file_exists($folder.$year.'/'.$month)){

mkdir($folder.$year.'/'.$month,0755);
}

$mypdf = $_FILES['pdf'];
$ext = substr($img['name'],-3);
$f['pdf'] = $year.'/'.$month.'/'.$f['url'].'.'.$ext;
move_uploaded_file($mypdf['tmp_name'], $folder.$year.'/'.$month.'/');

}



The mkdir to create the directory is working fine but the pdf its not uploading.



And Im getting this errors:



Warning: move_uploaded_file(): The second argument to copy() function cannot be a directory in move_uploaded_file($mypdf['tmp_name'], $folder.$year.'/'.$month.'/');



Warning: move_uploaded_file(): Unable to move 'F:\Xampp\tmp\php1BD6.tmp' to '../uploads/pdfs2014/04/' in move_uploaded_file($mypdf['tmp_name'], $folder.$year.'/'.$month.'/');


Answer



You need to specify a filename to the move_uploaded_files function as well, so it knows where to save the file(directory) and under what filename.




move_uploaded_file($mypdf['tmp_name'], $folder.$year.'/'.$month.'/'."my_newly_uploaded_file.pdf");


or if you want to keep the original filename



move_uploaded_file($mypdf['tmp_name'], $folder.$year.'/'.$month.'/'.$mypdf["name"]);

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...