PHP rename file on same host - php

$url = 'http://site.com/images/name.jpg';
Its a full path for the file, which is already exists on ftp.
How can I change file's name?

As others already point out, the PHP function you're looking for is rename, but you can't rename a file through a http:// URL (At least not in PHP - As #Artefacto says, WebDAV can do this.).
You will need to specify a proper filesystem path.

if your script on the server root (http://site.com/script.php) that script will do:
rename('images/oldname.jpg', 'images/newname.jpg');
since that's the relative path of the image from the script point of view.

Check out the rename() function. Seems to be what you're looking for here.
http://php.net/manual/en/function.rename.php

Related

file_put_contents could not store image on server

$path = 'http://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc6/372096_100002534902767_1927052265_n.jpg';
$info = file_put_contents('new/angel.jpg', file_get_contents(urldecode($path)));
echo $info;
It works fine on localhost but it did not work on my website.
Any idea what the problem might be?
Check your logs for error messages
Does the folder ("new") exist?
Are permissions set to allow writing by scripts?
Are you sure the error is in file_put_contents? file_get_contents could fail if the host has disallowed url_fopen.
unless $path is actually hardcoded, you will probably introduce an arbitrary file disclosure security issue. Make sure you validate your input.
you need to give a permission (write ) to upload folder
you can do that by the FTP program by using this codes in numeric value :777
Make sure that there are write permissions to the file in which you are writing
I think the relative path is the problem. It can possible that your local has the path to that file but the path does not exists on server.
3.Please put a forward slash in line $info=file_put_contents('new/angel.jpg', file_get_contents(urldecode($path))); before your "new" folder and try i think this might be a problem

How to change a filename of a file which is in a folder in PHP

Can anyone tell how to change a filename of a file which is a located in a folder using PHP?
rename("http://localhost/DXXX/photos/".$photoNamepart, "http://localhost/DXXX/photos/".$phototmpNamepart);
Error:Message: rename() [function.rename]: http wrapper does not support renaming
I guess the rename function could help.
rename("/tmp/tmp_file.txt", "/tmp/my_file.txt");
You're using rename wrong here. fopen, file_get_contents work fine with URLs - but that's more out of convenience rather than correctness.
For the code that you've written - you first need the absolute path of the file that you want to rename. It will work only on the local machine and on files that your webserver is configured to have write-access on.
Let's say your web server root is WEB_ROOT,
rename(WEB_ROOT."/DXXX/photos/".$photoNamePart, WEB_ROOT."/DXXX/photos/".$photoTempNamePart;
should do the trick.
rename($_SERVER['DOCUMENT_ROOT'].'/dir1/abc.png', $_SERVER['DOCUMENT_ROOT'].'/dir2/abc.png')
Above code works for me on CI
shell_exec('mv former_filename new_filename');
you should be having appropriate permissions to do this
This will works properly
copy(getcwd()."/tmp/tmp_file.txt", getcwd()."/tmp/my_file.txt");

How do I get a file's path in PHP?

I have to check using the file_exists function...
But, if I use something like that
if (file_exists('http://horabola.com/imagens/dt_2845.jpg')) {
//code
}
it doesn't work...
I know and I'm sure that the file "dt_2845.jpg" exists in the folder "imagens" ....
now, how do I check that? How do I get the server's file path?
Try:
if (file_exists($_SERVER['DOCUMENT_ROOT'].'/imagens/dt_2845.jpg')) {
//code
}
Good luck
The server's filesystem path for url / is stored in the $_SERVER['DOCUMENT_ROOT'] predefined variable.
The current script's path is always stored in the __FILE__ constant. You may want to use
dirname(__FILE__);
to know the current script's filesystem path.
What you tried by calling:
file_exists('http://horabola.com/imagens/dt_2845.jpg');
which is open files via HTTP, needs the use of fopen() instead of file_exists() (thanks #Gordon for pointing this out), and you need, on your PHP server, the so-called url wrapper for fopen(). Anyway, using the HTTP protocol to open files on the same server as the runnning script is a bit of a performance waste (using HTTP, that is network, instead of hard disk, that is 10x to 1000x faster).

php uploading file?

I just want to know that if I am using move_uploaded_file function and use two argument first as the name of file and second as the destination.
Normally I have uploaded many files with class uploader but now I want to give the destination as http://www.example.com/testing/
Although I have given 777 permission to this folder but when I try to execute the upload code error came
Destination directory can't be created. Can't carry on a process.
How can I upload the file local to server using php code?
If you are passing http://www.mydomain.com/testing/ as the target, this is wrong.
You can't just upload files to servers via HTTP, you only can do that to local folders, can you paste the exact code so we can know better what are you trying to do?
move_uploaded_file is a server-side function, so all the paths should be specified server side.
If your upload.php (i'm assuming the filename) is in the main directory of the website www.mydomain.com/ which is probably /home/youruser/public_html/ then you can specify the destination as simply "testing/"
If your upload file is in some nested directory, then it may work better to specify the full destination path:
/home/youruser/public_html/testing
good luck

move_uploaded_file not working

I'm uploading files via JS and storing the temp path in the session.
Than i use the following code to move the files.
if(move_uploaded_file($_SESSION['temp_img'][$key]['path'], $dest.$bigimg)){
$dest and $bigimg are defined earlier in the script with the id from the database.
Any Ideas or alternatives ?
MANCHUCK's answer was close but not quite there. You must call move_uploaded_file within the script where the file was uploaded. You cannot do what you're doing, that is, "storing temp path in the session" because that path is only valid for one request.
From the PHP manual:
The file will be deleted from the
temporary directory at the end of the
request if it has not been moved away
or renamed.
(Emphasis mine)
move_uploaded_file checks that a file has been uploaded to that page. You are actually uploading the file to a different PHP script then storing in a session. Instead of using move_upload_file use rename.
What is the output of $_SESSION['temp_img'][$key]['path'], also do you have permission to write to the web directory your placing the files. You may need to set it to 777 for some hosts to allow the webserver to write there.

Categories