Move file to specific path in PHP - php

I'm building a web app with MySQL/PHP/JS.
I have files in htdocs/mywebsite/foo/foo1/ path. Example:
htdocs/mywebsite/foo/foo1/image.png
I need to move these files to:
htdocs/mywebsite/foo3/image.png
Any help?

There is a rename() function in PHP that will just rename a file or will move it from one directory to another if the from and to directories are different.
<?php
rename("htdocs/mywebsite/foo/foo1/image.png",
"htdocs/mywebsite/foo3/image.png");
?>

In order to move files on a fileserver you need to use FTP (File Transfer Protocol). Programs like FileZilla could help you with that. A proper webhost should also have a FTP client on the control panel.
Using FileZilla:
Login to your webserver, simply cut and paste or drag and drop.

if you need to simple move all files from FTP using FTP tools like filezila or many other FTP software.
second from putty use mv command to move file.

Related

transfer file from path to path in ftp using php

Here is my prob in Brief.
I know how to upload a file to server using FTP with programming language PHP.
But is that possible to transfer files from one directory to another directory. In the same server....
I have the two paths of the directorys...
Any idea how?

PHP cURL get file with only a partial filename

I'm pretty new to the cURL library so I hope the solution to this isn't too trivial.
Basically I have a remote directory, lets say http://www.something.com/dir. In this directory following files are present:
file1.xml
file2.xml
file3_active.xml
Is there a way I can get the files where the filename matches the phrase 'active' into a string? Would the solution work both for http and ftp?
EDIT: How can I get the list/array of filenames in a remote dir? If I could do that, I could simply use strpos, get the full filename and use cURL the simple way.
Many Regards,
Andreas
How can I get the list/array of filenames in a remote dir.
Off the top of my head:
Via an FTP dir command, if you have FTP access.
Via a custom PHP (or whatever) script on the remote server which generates a machine-parsable list for you.
Via a shellexec/popen/ssh_exec to a shell command like ls or find, run through SSH.
By parsing HTML from a web-server generated directory listing (i.e., as generated by Apache mod_autoindex) on the remote server.
Each of these options is going to require some action on the part of the person hosting the remote server -- so if it's completely out of your control, I think you're SOL.

PHP Download file from non-hosted directory

I've been able to upload a file through PHP to a non-hosted directory (directory is not on the website) with read/write permissions for the PHP file (www). How would I download the file from this directory? I've been able to list the contents of the directory, but clicking on the files (made the filenames links) does not work as the computer attempts to download the file from the path on the server. I'm new to PHP, so all help is appreciated. Thanks!
Edit: Before I get down votes for this being a broad question, I just want to know how to access the files in the non-hosted directory and pass them to the user. I already know how to download normal files hosted on the website. Thanks!
You can use a delegating PHP file for file access. I don't know anything about your structure, but if you can write it, you can (presumably) read it back with file_get_contents:
<?php
echo file_get_contents('/the/path/to/the/unhosted/directory/file.ext');
?>

how to save uploaded file in another server?

I have tow servers for my web site. first, for database and php files. the second, for save useres' uploaded files.
So, if I uploade a file in server-1 xxx.com. how could i save it in server-2 yyy.com??
if you want two servers to be exact clones (contianing same files) you can run a rsync script after your first uplaod has completed. Its very easy and best of you don't have to specify files.
Lets say you want to transfer all files in directory /files/ to server2 in directory /files/2/ You can run this :
rsync /files/ yyy.com:~/files/2/
If you ONLY want specific files (extensions) to be synced, you can do this:
rsync /files/*.mp3 yyy.com:~/files/2/
The above will move ONLY MP3.
You can simply upload one file from server 1 to the server 2 using PHP's FTP functions.
See code example here: http://www.jonasjohn.de/snippets/php/ftp-example.htm
Use shared storage (SAN). Or SMB shares if on Windows. Or NFS if on Unix. Or scp (ssh) with public key authentication if on Unix.
An ugly way I used once was to pass via cURL and FTP commands
Of you course, you need to have access to your server-2 FTP...

Combine multiple files and folders into one file using PHP

I have designed an online editor with which a user can create files and folders on the server. Suppose a user creates the below files and folders.
/docs/abc.txt
/docs/def.txt
/docs/work/assignment.txt
I want to write a PHP script to combine the files into a single ZIP file so that it makes it easier for the user to download them with one click.
The only purpose is that I should get a single file with the directory hierarchy maintained which the user can download and uncompress on his system to get the original files and directory structure back. I don't care even if the ZIP file is not compressed.
Note that I am using a shared server and do not have access to execute external commands.
ZipArchive class is what you need. Using this you can add directories and file to an archive.
This is a common requirement and has been solved. Try and check out this class: http://www.phpclasses.org/browse/package/2322.html
More tutorials at:
http://davidwalsh.name/create-zip-php <-- this has a similar example if you scroll down
http://www.w3schools.com/php/php_ref_zip.asp
Assuming you have permission to execute an external command, you could use exec to run an external command-line capable ZIP program such as 7-Zip.

Categories