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.
Related
My aim is to download multiple files into the folder on my localhost. I am uploading them using the HTML form.
Here is the code (really sorry that I can't give a link to the executable version of the code because it relies on too many other files and database if anyone knows the way then please let me know)
foreach ($_FILES as $value) {
$dir = '/';
$filename = $dir.basename($value['name']);
if (move_uploaded_file($value['tmp_name'],$filename)) {
echo "File was uploaded";
echo '<br>';
}
else {
echo "Upload failed";
echo '<br>';
}
}
So this little piece of code give me an error:
And here are the lines of code:
The problem is that the adress is correct, I tried enterring it into my file directory and it worked fine, I have seen some adviced on other people's related questions that // or \ should be used instead, but my version works just fine! Also I have checked what's inside the $_FILES and here it is if that's required for someone trying to help:
Thank you very much if anyone could help!!
You are trying to move the file to an invalid (or non-existent) path.
For the test you will write
$dir = 'c:/existing_dir/';
$filename = $dir.basename($value['name']);
If you want to move the file to a folder that is relative to the running file try
$dir = '../../directory/';// '../' -> one directory back
$filename = $dir.basename($value['name']);
By starting your file path with $dir = '/'; you are saying store the file on the root folder, I assume of C:
Apache if correctly configures should not allow you access to C:\
So either do
$dir = '../';
$filename = $dir.basename($value['name']);
to make it a relative path or leave the $dir = '/'; out completely
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 am trying to create couple of lines of code which will rename all the jpg images in a specified directory.The parent directory was c:/xampp/htdocs/practice/haha/.But renamed images are saved in c:/xampp/htdocs/practice/
My code do rename the file ,but problem is it deletes all the image file form the specied directory leaving the directory empty.It stores the renamed file in root php directory.And the newly created files are not clickable image file.So there are two problem i want to solve.
how can i keep the renamed files in the same directory where they were before any renaming occur.
2.How i can sustain them to be a clickable image file?
this is how they looked after renaming:
$dir='c:/xampp/htdocs/practice/haha/';
echo getcwd().'</br>';
$i=1;
if(is_dir($dir)){
echo dirname($dir);
$file=opendir($dir);
while(($data=readdir($file))!==false){
$info=pathinfo($data,PATHINFO_EXTENSION);
if($info=='jpg'){
//echo pathinfo($data,PATHINFO_BASENAME).'</br>';
echo basename(pathinfo($data,PATHINFO_BASENAME),'.jpg').'</br>';
rename($dir.'/'.$data,'image'.$i.'jpg');
$i++;
}
}
}
You missed a dot in the file name:
rename($dir.'/'.$data,'image'.$i.'jpg');
This is why they are not clickable. Use this instead:
rename($dir . '/' . $data, 'image' . $i . '.jpg');
// --------------------------------------- ^
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"]";
When I upload a file using this code, it puts a copy of the file in the "uploads" folder(which is what I want) but it also puts a copy in my root. I only want the files going to the uploads folder.
define ('GW_UPLOADPATH', 'uploads/');
$upfile= GW_UPLOADPATH . $_FILES['userfile']['name'];
if(is_uploaded_file($_FILES['userfile']['tmp_name']))
{
if(!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile)) //this is saying if the file isn't moved to $upfile.
{
echo 'Problem: could not move file to destination directory';
exit;
}
}
else
{
echo 'Problem: Possible file upload attack. Filename: '; //this could be an attack b/c it might be from localhost.
echo $_FILES['userfile']['name'];
exit;
}
echo 'File uploaded successfully<br><br>';
What would be your temporary dir? Is it possible that somehow the uploaded file lands in the root but PHP can not delete it? Figuring this out requires a lot more knowledge about your setup.