PHP file upload image not loading to server - php

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

Related

photo uploading in php under centos platform

While loading the photo using the code
if (file_exists($DocRoot. $_FILES["file"]["name"]))
{
$sourcePath = $_FILES['file']['tmp_name']; // Storing source
path of the file in a variable
$image_name = $_SESSION['d'];
$targetPath = $DocRoot.$image_name; // Target path where
file is to be stored
move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file
}else{
$sourcePath = $_FILES['file']['tmp_name'];
$image_name = $_SESSION['d'];
// Storing source path of the file in a variable
$targetPath = $DocRoot.$image_name; // Target path where
file is to be stored
move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded
file
the following error occurs:
Array ( [type] => 2 [message] => move_uploaded_file(): Unable to move '/tmp/phpihV9Ar' to '/var/www/html/test963853/q1/cas/upload/4442910001.jpg' [file] => /var/www/html/test963853/q1/cas/stage1_1.php [line] => 1960 ) /var/www/html/test963853/q1/cas/upload/4442910001.jpg
Experts state that there would be some problem with httpd.conf. What modification must be carried out in httpd.conf? Please suggest. Thank you in advance.

PHP move_uploaded_file Rename file

I know that move_uploaded_file() sets the name of the uploaded file and sets the destination also. I have this:
$sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable
$targetPath = $_SERVER['DOCUMENT_ROOT'] . '/img/profiles/'.$_FILES['file']['name']; // Target path where file is to be stored
move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file`
I have tried explode $_FILES['file']['tmp_name'] but I don't get to change the name of my uploaded file to my POST variable $newfile=$_POST["something"];
Thank you in advance
I am using
//File name
$file_name = $_FILES["file"]["name"];
$file_name = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file_name);
// get extension
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
// change name
$imagename = $file_name . time() . "." . $ext;
Set the entire upload path with the file name in $targetpath variable.
In you code
$sourcePath = $_FILES['file']['tmp_name'];
$newfile=$_POST["something"]; //any name sample.jpg
$targetPath = $_SERVER['DOCUMENT_ROOT'] . '/img/profiles/'.$newfile;
move_uploaded_file($sourcePath,$targetPath) ;
Now the uploaded file name is sample.jpg
I think this will helpful for you.

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 move uploaded files to random folder

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

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"

Categories