Im learning PHP by myself from a book called "PHP the Complete Reference - PHP5.2" Im currently at Chapter11 FTP, uploading,deleting,makedir etc but have run into a few questions not covered by the book:
According to my textbook this is a simple code to upload to a server:
$connect=ftp_connect("johnsite.com");
$result=ftp_login($connect,"john@johnsite","johnnyWalker");
if(!$result){
echo'Could not connect to Server';
}
$result=ftp_put($connect,'myFile.php',FTP_ASCII);
echo'UPLOADING FILE......';
if($result){
echo'File Uploaded!';
}
My Questions:
- To which directory will this upload, how will I modify code if i want to upload to directory say
public_html/images/myFile.jpg
In the example myFile.php is hardcoded, what if I want user to select a file to upload? Am I correct in assuming you can do something like this:
<input type="file" name="myFile" value="upload a file" /> <input type="submit" name="upload" /> if(isset($_POST['upload'])){ $fileName=$_POST['myFile']; //file is now assigned to var name $result=ftp_put($connect,$fileName,FTP_ASCII); //file linked to var name being uploaded }
Is this the most efficient secure way?
Thank you for reading
As @Bonner said Fabien answer is incorrect since you are looking for a script to upload files from a page on your website to the server.
First thing to remember is that ftp_put() function will always overwrite the existing files. Instead I suggest you to have a look at the PHP move_uploaded_file
Code
This is the form. Inside the action attribute we specify a file, which will handle and process all the files. You'll need to use the multipart/form-data value for the form's enctype property.
I have included comments almost everywhere for a better understanding.
upload.php
1. If you want upload to directory public_html/images/
2.