//=============== PDF File Upload=====================
if (isset($_FILES["pdfile"])) {
$filename = $_FILES['pdfile']['name'];
$dir = "../pdfs/students/".$filename;
$tmp_name = $_FILES['pdfile']['tmp_name'];
$file1 = explode('.',$filename);
$ext = $file1[1];
$allow = array('pdf');
if(in_array($ext, $allow))
move_uploaded_file($filename, $dir);
}
else
die("There is no file to upload.");
//=============== End Pdf File Upload=====================
//=============== Image File Upload=====================
if(isset($_FILES['profile']['tmp_name'])){
$dir = "../images/students/".$id.".jpg";
$file = $_FILES['profile']['tmp_name'];
if(move_uploaded_file($file, $dir)){
echo "Image Uploaded Successfully";
$pic = mysqli_query($con, "update students set profile ='$id.jpg' where id = $id ")or die(mysqli_error($con));
}
else{
echo "Error Occured";
}
}
//=============== End Image File Upload=====================
PDF File didn't get upload in directory. I don't know why and same code for profile picture uploading works fine. Help me with this guys.
neubert
17k28 gold badges140 silver badges264 bronze badges
-
What is the output of the script? What text are you seeing?MaartenDev– MaartenDev2022年05月14日 20:50:22 +00:00Commented May 14, 2022 at 20:50
-
output of the script says pdf file uploaded successfully but when i checked in that directory there is no file showed up.Beast69– Beast692022年05月14日 21:05:26 +00:00Commented May 14, 2022 at 21:05
-
stackoverflow.com/a/72243993/6127393Arleigh Hix– Arleigh Hix2022年05月14日 21:41:46 +00:00Commented May 14, 2022 at 21:41
3 Answers 3
Sorry I didn't see at first time at my code. the error is of $tmp_name at move_upload_file($filename, $dir). Replace $filename to $tmp_name and it works fine.
//=============== PDF File Upload=====================
if (isset($_FILES["pdfile"])) {
$filename = $_FILES['pdfile']['name'];
$dir = "../pdfs/students/".$filename;
$tmp_name = $_FILES['pdfile']['tmp_name'];
$file1 = explode('.',$filename);
$ext = $file1[1];
$allow = array('pdf');
if(in_array($ext, $allow))
move_uploaded_file($filename, $dir);
}
else
die("There is no file to upload.");
//=============== End Pdf File Upload=====================
Sign up to request clarification or add additional context in comments.
Comments
Change this line
move_uploaded_file($filename, $dir);
To this
move_uploaded_file($tmp_name, $dir);
answered May 14, 2022 at 21:30
Maharramoff
1,05910 silver badges15 bronze badges
Comments
if (isset($_FILES["pdfile"])) {
$filename = $_FILES['pdfile']['name'];
$dir = "../pdfs/students/".$filename;
$tmp_name = $_FILES['pdfile']['tmp_name'];
$file1 = explode('.',$filename);
$ext = $file1[1];
$allow = array('pdf');
if(in_array($ext, $allow))
move_uploaded_file($filename, $dir);
}
move_uploaded_file should be:
move_uploaded_file($tmp_name, $dir);
more tips:
<?php
if (isset($_FILES["pdfile"])) {
$filename = $_FILES['pdfile']['name'];
$tmp_name = $_FILES['pdfile']['tmp_name'];
$dir = "../pdfs/students/" . $filename;
$file1 = explode('.',$filename);
// use end() to get last
// for example: filename.jpg.pdf
$ext = end($file1);
$allow = array('pdf');
if(in_array($ext, $allow))
move_uploaded_file($tmp_name, $dir);
}
answered May 14, 2022 at 21:26
obeid salem
1452 silver badges14 bronze badges
2 Comments
Beast69
ThankYou, I just saw this error couple of mins ago.
obeid salem
np .. u can see the tips for ext.
lang-php