move_uploaded_file($fileSrc, $target); is not working, I get the image name and extension of the image but image is not going to target folder. I don't know why this is not working. I am working on xampp localhost
if(isset($_FILES["filesToUpload"]["name"]))
{
$name=$_FILES['filesToUpload']['name'];
$fileSrc=$_FILES['filesToUpload']['tmp_name'];
$extnsion= pathinfo($name, PATHINFO_EXTENSION);
$fileName = "tourGuides_" . $id . '.' . $extnsion;
$target="../img/guide_profile/".$fileName;
move_uploaded_file($fileSrc, $target);
}
The thing you forgot, is that you cannot move a file based on just its name. You should also provide PHP with the location of that file.
This can be as simple as:
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); // or tmp_name
$target="../img/guide_profile/".$fileName;
move_uploaded_file($target_file, $target);
Related
I have a php script which uploads files:
for($index = 0;$index < count($_FILES['files']['name']); $index++){
// File name
$filename = $_FILES['files']['name'][$index];
// File path
$path = '../pdf/'.$filename;
// Upload file
if(!move_uploaded_file($_FILES['files']['tmp_name'][$index],$path)){
// ERROR
exit;
}
This works fine!
But I would like to modify the filename while uploading:
For example:
I uploading a "upload.pdf" file.
But I would like to get the name "upload_2021.pdf"
How can I realize it?
I found a solution !!
$path = '../pdf/'.$filename;
$extension = pathinfo($path, PATHINFO_EXTENSION);
$name = pathinfo($path, PATHINFO_FILENAME);
From what I can see, the only thing you need to do is change the $path variable.
Bare in mind, that what you are showing here is not a safe and secure upload script. You are not doing any file and mime-type validation what so ever. There is nothing preventing your users from uploading harmful files. Together with not checking for duplicate files and some other edge cases, think twice before putting this into production.
for($index = 0;$index < count($_FILES['files']['name']); $index++){
// File name
$filename = $_FILES['files']['name'][$index];
$parts = explode(".", $filename);
$year = date("Y");
// File path
$path = '../pdf/'.$parts[0] . '_' $year . '.' . $parts[count($parts)-1];
// Upload file
if(!move_uploaded_file($_FILES['files']['tmp_name'][$index],$path)){
// ERROR
exit;
}
}
a short example:
$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);
I'm trying to create a file upload only my current script doesn't seem to work as I believe it should.
I've managed to get the data saved in the MySQL table okay but I can't seem to get the file into the 'uploads' directory?
if(isset($_POST['new']) && $_POST['new']==1){
$folder = "uploads/";
$upload_image = $folder . basename($_FILES["image1"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $upload_image);
$trn_date = date("Y-m-d H:i:s");
$brand =$_REQUEST['brand'];
$user =$_SESSION["username"];
$model = $_REQUEST['model'];
$serial =$_REQUEST['serial'];
$purchasedate = $_REQUEST['purchasedate'];
$img1 =$_REQUEST['image1'];
$ins_query="insert into table
(`user`,`trn_date`,`brand`,`model`,`serial`,`purchasedate`,`image1`)values
('$user','$trn_date','$brand','$model','$serial','$purchasedate','$img1')";
mysqli_query($con,$ins_query)
or die(mysql_error());
$status = "added successfully.
</br></br><a href='home.php'>home</a>";
}
$upload_image = $folder . basename($_FILES["image1"]["name"]);
move_uploaded_file($_FILES["image1"]["tmp_name"], $upload_image);
What if trying directly what the doc said ;)
$uploads_dir = '/uploads';//try over 'uploads' too or create a 'uploads' folder in you app_root
foreach ($_FILES["image1"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["image1"]["tmp_name"][$key];
$name = $_FILES["image1"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
Possible Link
I have found an issue in your code.
$folder = "uploads/";
$upload_image = $folder . basename($_FILES["image1"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $upload_image);
In this case the file does not have the extension
$folder = "uploads/";
$ext = explode('.', basename( $_FILES['image1']['name']));
$upload_image = $folder . basename($_FILES["image1"]["name"].$ext);
move_uploaded_file($_FILES["image1"]["tmp_name"], $upload_image);
The uploads folder is in the same directory that is the php script?
You also can verify if the file was correctly moved doing the following test
if (file_exists($upload_image)){
echo "true";
}
Provide a file upload validation before uploading into server it will prevent server from malicious files
How to give file validation in php : Link
The following code simply uploads two files. I want to put both files in a unique folder. The code does create a random folder, however, how can I move the uploaded files into that folder?
.php
<?php
$new_image_name = uniqid("IMG_",false);
$target_dir = uniqid();
mkdir("C:/xampp/htdocs/uploads/" . $target_dir);
//I just put random_dir here as a placeholder
$target_file = $random_dir . $new_image_name . basename($_FILES["fileToUpload"]["name"]);
$target_file2 = $random_dir . $new_image_name . basename($_FILES["fileToUpload2"]["name"]);
if ( $_FILES["fileToUpload"]["tmp_name"] ) move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
if ( $_FILES["fileToUpload2"]["tmp_name"] ) move_uploaded_file($_FILES["fileToUpload2"]["tmp_name"], $target_file2);
?>
I have a script which is uploading and saving video's correctly but I want to rename the video before saving.
The page upload-video.php ends in .php?video_id=556, in this example I want to save the video as 556
$video_id=$_GET["video_id"];
$target_dir = "video_uploads/";
And the move script:
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file
Can anyone advise?
You can use
$target_file = $target_dir . $video_id;
to define the destination properly before your move_script.
In this line of code:
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
$target_file is the name of the file being saved on the server. You can use whatever value you like there. So if you want to save it as 556 then you'd use that as the file name. For example:
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], '/path/to/videos/556');
Whatever logic you want to use to determine the file name would be the logic you use to build the $target_file variable.
$video_id=$_GET["id"];
$path = $_FILES["fileToUpload"]["name"];
$ext = pathinfo($path, PATHINFO_EXTENSION);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "video_uploads/$video_id.$ext"
Having some trouble with rewriting a photo file. I need the file name to get rewritten as a random string. The file uploads fine - I can't seem to get it copy the file and rewrite the file name to the random string. The file is going to stay in the directory.
The function is working fine and I can rewrite file name in the database, but it will not rewrite the actual file in the folder. The folder permissions are rwxr-xr-x (755).
Any thoughts?
function AfterUpdate(){
$file = $this->file_attachment;
$path_parts = pathinfo($file);
$newFilename = $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];
$file_src = $_SERVER['DOCUMENT_ROOT'] . $file;
$newfile_src = $_SERVER['DOCUMENT_ROOT'] . $newFilename;
if (move_uploaded_file($file_src, $newfile_src)){
$this->file_attachment = $newFilename;
}
}
$newFilename contains a path location I guess by looking at the $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];.
$newFilename should just be the new file name with extension.
move_uploaded_file will only move files from one folder to another or the same, that already exists. But will not create a folder for you.
Simple fix. Replace move_uploaded_file with rename. The file will not be moved, just renamed.
$file = $this->file_attachment;
$path_parts = pathinfo($file);
$newFilename = $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];
$file_src = $_SERVER['DOCUMENT_ROOT'] . "/" . $file;
$newfile_src = $_SERVER['DOCUMENT_ROOT'] . "/" . $newFilename;
if (rename($file_src, $newfile_src)){
$this->file_attachment = $newFilename;
}