I have a problem when I want to copy a file from one folder to another on my website, I don't know what I'm doing wrong,the file is called "installed" and dont have extension, is only archive,I need to copy this file from the "ins" folder to the "var" folder this is the php code I'm using:
<?php
$file = '/../domains/travianforce.com.es/public_html/ins/installed';
$new_file = '/../domains/travianforce.com.es/public_html/var/';
if (!copy($file, $new_file)) {
echo "Error to copy $file...\n";
}
?>
but when I run the script it gives me an error, I don't know what I'm doing wrong but I need your help, thank you very much! =)
Give full path in $file and $new_file
And ans already given in this
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'm creating a PHP script, which supposed to extract a zip archive stored on the php file directory to a folder.
Everything works well, but when I check te result, I find 2 folders under the directory: a folder with the name of the zip archive, and another folder named __MACOSX. I don't know how this folder came there, especially as I'm using Windows 7. Second, in each folder there is a file called .DS_Store.
Now, I don't know how these things got there. This is my code:
$zip = new ZipArchive;
if ($zip->open('File.zip')) {
$path = getcwd() . "/details/" . trim($id) . "/";
$path = str_replace("\\","/",$path);
echo $path;
echo $zip->extractTo($path);
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
This is the only code that extracts the zip file, or touching it, and as you can see, there is nothing like __MACOSX or .DS_Store.
Can you please help me?
File.zip originated on a OSX system. __MACOSX and .DS_Store have 0 usage or bearing on any other OS. Delete / Ignore them and keep trucking.
As an aside, you may want to add the stated file system objects to your project .gitignore.
https://superuser.com/questions/104500/what-is-macosx-folder
https://en.wikipedia.org/wiki/.DS_Store
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.');
}
my code keeps on saying that the folder does not exist though it should be checked by the mkdir function..it creates the folder but does not go through the uploading process.. and displays the error on the couldnt find the folder.. is the algorithm correct? please help.. Your advice will help! :)
here is the code..
if(!(file_exists($target_path)))
{
if(!mkdir($target_path, 0777, TRUE))
{
die ("could not create the folder on mkdir");
}
//in this line the error occurs..printing what is below..//
die ("could not find folder on file exists");
}
else
{
umask($target_path);
...
}
file_exists() routine requires the complete path to file like
/var/www/uploads/file1.c
so the
file_exists($target_path);
call is ok . but second call to make directory, ie,
mkdir()
is requiring an directory , not an path to an file ie, it require /var/www/upload part only .
so you can remove the basename from path name and apply it to mkdir function()
try..
if(file_exists($target_path) && is_dir($target_path)){ //rest of the code...
}
instead of...
if(!(file_exists($target_path))){
}
Hope this will do something for you...
...............................
one thing more...
i think the problem is with if(!(file_exists($target_path))){} Statement,
THIS SHOULD BE...
if(!file_exists($target_path)){}
during the file upload process, your file saving path in move_uploaded_file()
function may be creating problem. I am saying may be because your given code is not clear enough to me. Second parameter of move_uploaded_file() is the destination where first parameter is the file name. please check the value of $target_path, it may solve your problem. thank you.
I have a file called filecontent.txt which includes some php code.
And i have another file called index.php,
I want to replace the index.php content with the filecontent.txt content.
Is there any solution for it??
Please help me on this.
You can use php copy function
eg:
<?php
$file = 'example.txt';
$newfile = 'example.txt.bak';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>
PHP COPY
You may also delete the index.php and rename the filecontent.txt to index.php afterwards, if its just a replacement.
unlink ("index.php");
rename ("filecontent.txt", "index.php");