I used a file upload form to upload files to my server. Then I echo $_FILES["file"]["tmp_name"] but it returns to me a location that does not seem to exist. eg /tmp/phpBparj4 is the output but there is no such file/folder in my /tmp folder. I have also checked for the appropriate permissions for the folder
My actual concern is move_uploaded_file($_FILES["file"]["tmp_name"],$target); which is not moving the uploaded file to the target location. I have also tried move_uploaded_file($_FILES["file"]["tmp_name"].$file_name,$target);
I am not sure if I understand right but why can't you specify the file folder location? i.e."
//set the right path from the server perspective
$article_file_path = $_SERVER['DOCUMENT_ROOT'];
// you should check for the file if it is/ or not already there (something like this)
if (!file_exists($article_file_path ."/your_folder/file_subfolder/". $_FILES["my_file"]["name"]))
{ do something.....
// then make your script upload the file exactly where you want it
move_uploaded_file($_FILES["my_file"]["tmp_name"],
$article_file_path ."/your_folder/file_subfolder/".$_FILES["my_file"]["name"]);
// and the download link to the uploaded file would be something like this:
echo "http://". $_SERVER["HTTP_HOST"] . "/files-article/".$_FILES["my_file"]["name"]";
Related
I am having a problem with move_uploaded_file().
I am trying to upload a image path to a database, which is working perfectly and everything is being uploaded and stored into the database correctly.
However, for some reason the move_uploaded_file is not working at all, it does not produce the file in the directory where I want it to, in fact it doesn't produce any file at all.
The file uploaded in the form has a name of leftfileToUpload and this is the current code I am using.
$filetemp = $_FILES['leftfileToUpload']['tmp_name'];
$filename = $_FILES['leftfileToUpload']['name'];
$filetype = $_FILES['leftfileToUpload']['type'];
$filepath = "business-ads/".$filename;
This is the code for moving the uploaded file.
move_uploaded_file($filetemp, $filepath);
Thanks in advance
Try this
$target_dir = "business-ads/";
$filepath = $target_dir . basename($_FILES["leftfileToUpload"]["name"]);
move_uploaded_file($_FILES["leftfileToUpload"]["tmp_name"], $filepath)
Reference - click here
Try using the real path to the directory you wish to upload to.
For instance "/var/www/html/website/business-ads/".$filename
Also make sure the web server has write access to the folder.
You need to check following details :
1) Check your directory "business-ads" exist or not.
2) Check your directory "business-ads" has permission to write files.
You need to give permission to write in that folder.
make sure that your given path is correct in respect to your current file path.
you may use.
if (is_dir("business-ads"))
{
move_uploaded_file($filetemp, $filepath);
} else {
die('directory not found.');
}
I have a form with a file to uplaod. All works find. But I don't want to move the file directly into a folder.
After submit I show a confirm page and there I show the uploaded file with
header('Content-Type: image/x-png');
$file = file_get_contents(\Illuminate\Support\Facades\Input::file('restImg'));
$imgType = \Illuminate\Support\Facades\Input::file('restImg')->guessClientExtension();
echo sprintf('<img src="data:image/png;base64,%s" style="max-height: 200px"/>', base64_encode($file));
This works fine. After the confirmation I like to move the file to a folder. How can I move the file after the confirmation? The Input::get('file') is not available anymore.
You will have to store the file in the initial upload somewhere temporarily other than the default tmp directory.
The documentation for PHP file uploads says:
The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed
This means that moving onto the next request, the file will no longer be available.
Instead, move it to your own custom temp directory or rename it to something special, then keep the filename in the $_SESSION to persist it to the next request.
For Laravel, this should mean putting it in the /storage directory with something like this:
// Get the uploaded file
$file = app('request')->file('myfile');
// Build the new destination
$destination = storage_path() . DIRECTORY_SEPARATOR . 'myfolder';
// Make a semi-random file name to try to avoid conflicts (you can tweak this)
$extension = $file->getClientOriginalExtension();
$newFilename = md5($file->getClientOriginalName() . microtime()).'.'.$extension;
// Move the tmp file to new destination
app('request')->file('myfile')->move($destination, $newFilename);
// Remember the last uploaded file path at new destination
app('session')->put('uploaded_file', $destination.DIRECTORY_SEPARATOR.$newFilename);
Just remember to unlink() the file after the second request or do something else with it, or that folder will fill up fast.
Additional Reference:
http://api.symfony.com/2.7/Symfony/Component/HttpFoundation/File/UploadedFile.html
I'm trying to push a file to a Amazon s3 filebucket.
I'm posting the file through an html form.
I try to generate a path to the file like this($file is a part of a foreach, because i need to support multiple files in a form-submit.)
$file['tmp_name'].'/'.$file['name'];
this outputs a filepath like this
/Applications/MAMP/tmp/php/phpZDcVQv/pdf.pdf
/Applications/MAMP/tmp/php/ exists, but nothing is inside it. I have set access read and write for everyone to that folder.
I use a library to post the images to Amazon: https://github.com/tpyo/amazon-s3-php-class It also complains that the filepath i have provided doesn't exist. It's running a check like:
if (!file_exists($file) || !is_file($file) || !is_readable($file))
How come the files aren't added?
Am I referencing the wrong folder? The file with the code is in /web/projectname/
Someone on the internet said something unclear about php removing the temp-file directly. Is this after the response has been run? Do I need to address this in some way?
The most simple code that generates the problem:
foreach ($_FILES as $file) {
$filepath = $file['tmp_name'].'/'.$file['name'];
if(file_exists($filepath)){
echo 'true <br />';
}else{
echo 'false <br />';
}
}
This echo:es false even if files have been uploaded.
$filepath contains the path i described above.
as the manual states:
$_FILES['userfile']['tmp_name']
The temporary filename of the file in which the uploaded file was stored on the server.
and
$_FILES['userfile']['name']
The original name of the file on the client machine.
this means that file_exists($file['tmp_name']) should be true.
The path $file['tmp_name'].'/'.$file['name'] is bogus, since $file['name'] is there only for informing you of the original name, but this name is not used while saving the uploaded file on the server.
So in your example /Applications/MAMP/tmp/php/phpZDcVQv is actually the uploaded file.
I am trying to copy a uploaded file which is in my directory called "upload" and is called image (5).jpg to my other directory called "images". This is my code.
<?php
copy(upload/image (5).jpg, images/);
?>
Am I doing this right?
First your image file name is awful, please do not use spaces in file names. Also do not use brackets in file name. Some systems like google app engine would easily mark that as invalid file name and ignore it.
Then adjust a little your line of code to look like this:
$old_file = 'upload/image_5.jpg';
$new_file = 'images/image_5.jpg';
$copied = copy(old_file, $new_file);
if ($copied) {
print "file " . $old_file . " is copied to " . $new_file;
} else {
print "error";
}
If you encounter an error maybe paths to files are not configured right. You do not have a write permission on that specific directory etc.
I want to delete with php (unlink function) file which is out of webroot. my web root is in
C:\server\webroot\project\... in webroot I have folder named project and in there I have .php files.
whats about files directory. it is situated C:\server\mp3_files...
Also I've created in httpd.conf Alias("mp3") of mp3_files directory
I am writing this script in C:\server\webroot\project\test.php
script is like so =>
function delete($filename){
if (unlink("/mp3/" . $filename)){
echo "Deleted";
} else {
echo "No";
}
}
delete("file.txt");
this script gives me in php-errors => PHP-WARNING No such file or directory
also I have in (test.php) html form this =>
Download
And this works (It opens this file.txt)
So I'm wondered why can't delete with marked function "delete($filename)" ?
"/mp3/" . $filename is an absolute filepath, not relative to the webserver root, so it's assuming that you have an mp3 directory under your filesystem root when you should be looking under /server/mp3
EDIT
And is it /server/mp3 or /server/mp3_files
your post seems to contradict your code
File function in PHP go from the file system root.
You should write:
function delete($filename){
if (unlink("C:\\server\\mp3_files\\" . $filename)){
echo "Deleted";
} else {
echo "No";
}
}
delete("file.txt");
To make sure the internal PHP file path cache gets the correct information, reset with it with clearstatcache() before and after the unlink. Normally the path cache is reseted after every PHP function which is related to file manipulation. Reseting the cache is required if you remove files with shell_exec('rm file.txt') or similar.
See http://php.net/manual/ini.core.php#ini.realpath-cache-size and http://php.net/manual/ini.core.php#ini.realpath-cache-ttl