PHP move uploaded files to random folder - php

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

Related

Upload image file to server, store name in MySQL DB

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

PHP move_uploaded_file() function is not working

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

PHP file upload image not loading to server

I am attempting to upload a image file via php and it is not working:
<?php
$target_dir = "/home/NAME/uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES['fileToUpload']['name'], $target_dir);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
print_r($_FILES);
?>
This is what is returned, but no file actually uploads. Anyone know what is going on? Thanks
Array ( [fileToUpload] => Array ( [name] => followers.png [type] => image/png [tmp_name] => /tmp/phpKsuz1B [error] => 0 [size] => 127008 ) )
Change:
move_uploaded_file($_FILES['fileToUpload']['name'], $target_dir);
To:
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)
Change move_uploaded_file($_FILES['fileToUpload']['name'], $target_dir); to move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_dir);
move_uploaded_file needs the temporary file name in order for it to upload, not the original name of the file, since it needs a resource to move.
The uploaded file is actually $_FILES['fileToUpload']['tmp_name'], this is the file you need to move.
A good way to go about this is:
$tempFile = $_FILES['fileToUpload']['tmp_name'];
$destFile = '/dest/directory/' . $_FILES['fileToUpload']['name'];
// You'll now have your temp file in destination directory, with the original image's name
move_uploaded_file($tempFile, $destFile);
A good practice is to keep your file names unique, because you never know when different images may be named image01.jpg (more often than one would hope).
$tempFile = $_FILES['fileToUpload']['tmp_name'];
$destDir = '/dest/directory/';
$destName = uniqid() . '_' . $_FILES['fileToUpload']['name'];
$destFile = $destDir . $destName;
// Temp file is now in destination directory, with a unique filename
move_uploaded_file($tempFile, $destFile);

Rename Video on upload

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"

PHP - move_uploaded_file not working to copy file in same folder

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

Categories