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"
Related
I want to add validation or change the file name if there is a file name that already exists in the folder
$image = $_FILES['image']['name'];
$tmp = $_FILES['image']['tmp_name'];
$target_dir = "../file/upload/201902/28/";
move_uploaded_file($tmp, $target_dir.$image);
First of all validation and changing file name is not same...
You can find file name if exist in that particular folder by "file_exists" php function.
To avoid this problem, you need to take unique filename by various method...
You can take timestamp as filename....
$image = $_FILES['image']['name'];
$tmp = $_FILES['image']['tmp_name'];
$time=time();
$filename = $tmp."_".$time;
$target_dir = "../file/upload/201902/28/";
move_uploaded_file($filename, $target_dir.$image);
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);
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);
I apologize for the super long post. I'm super noob and I would LOVE to have your help.
I made a file called Upload Pictures and in this file I have four files. First, php.ini with the following code:
file_uploads = On
Second, upload.html:
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
Third, upload.php:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
And fourth, I have a folder called uploads, with nothing inside that folder.
My main questions are from the upload.php file. Please correct me if I am incorrect in my understanding.
I understand $target_dir to be the directory in which the files will be stored. Now to the line with &target_file. I understand that $_FILES["fileToUpload"]["name"] returns the name of the file that I am uploading. So if my file name was picture.jpg, it will return picture.jpg.
My question is then, why do I need to call the basename()? Because I thought this function just returns the name of the file. So it's almost like returning the name twice with $_FILES["fileToUpload"]["name"] and basename()?
Also, it has been my experience that having a . and some function after is calling a method from a class. Is that what is happening here $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);? If so, I'm not sure what is actually being stored into $target_file.
Also, switching gears a little bit here, when I pressed the Upload Image button from my html, it takes me to another page. Is there a way to prevent that from happening?
basename returns the actual filename from a path.
In your example basename does not need to be used on the name being retrieved from $_FILES because it will have no associated path to begin with. In an upload script basename may be more traditionally used on tmp_name to remove any temporary folders from the name of the file.
The . is how you concatenate strings (or in this case variables containing strings) in PHP.
In this example, the code is concatenating the directory and the name of the file together into a variable in which it is used to get the extension in the $imageFileType variable using pathinfo.
The form has an action attribute. action="upload.php" so you will be taken to the 'upload.php' page when the form is submitted.
I hope this answers your questions-
actually basename() function for get file name with the extension of file.
so basename function stand for
basename(path,suffix)
Explanation
Parameter | Description
--------------------------
path | Required. Specifies the path to check
suffix | Optional. Specifies a file extension. If the filename has this file extension, the file extension will not show
Ex
<?php
$path = "/testweb/home.php";
//Show filename with file extension
echo basename($path) ."<br/>";
//Show filename without file extension
echo basename($path,".php");
?>
Output will be
home.php
home
php 5 basename() function
and we use . (dot) operator on here, $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
so this will bind $target_dir and basename(). so $target_file knows where is file should be goes exactly.
I am using the code bellow to upload a file using php and inserting file name into database. Actually I want to rename of file on uploading and want to insert new renamed name into database. I know how to insert name into database but I don't know how to rename uploaded file name. Please help.
I am using code bellow:
$target = "uploads/";
$target = $target . basename( $_FILES['uploaded']['name']);
move_uploaded_file($_FILES['uploaded']['tmp_name'], $target);
$add_file = $_FILES['uploaded']['name'];
Thank you so much..
Is this what you are looking for?
<?php
rename("/tmp/uploaded_file.txt", "/home/user/login/uploaded/67A7466B576.txt");
?>
So new code will be:
$target = "uploads/";
$target = $target . basename( $_FILES['uploaded']['name']);
rename($_FILES['uploaded']['tmp_name'], $target);
$add_file_to_db = $target;
This might be helpful for you:
$uploaded_file = time()."__".$_FILES['uploaded']['name'];
This simply adds time before the name of the file.
Example:
If I uploaded the AnalysisReport.doc file, then it simply becomes like 1354173106__AnalysisReport.doc