I am having an issue with uploading to FTP from my website through PHP.
It connects OK, but then has an issue with the uploading of the image.
HTML
<form action="../scripts/php/saveupload.php" method="post">
<input name="file" type="file" />
<input name="submit" type="submit" value="Upload File" />
</form>
PHP
$ftp_server = "XXXXXX";
$ftp_user_name = "XXXXX";
$ftp_user_pass = "XXXXX";
$destination_file = "/public_html/img/news/";
$source_file = $_FILE['file']['tmp_name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
//check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
The message I am getting is:
"Connected to ftp.theucl.co.uk, for user theucl.co.ukFTP upload has
failed!"
This is now the error I am getting after following Niths advice on the error...
It appears the error is around the following..
$source_file = $_FILES['file']['tmp_name'];
and
$upload = ftp_put($conn_id, $destination_file.$_FILES['file']['tmp_name'], $source_file,FTP_ASCII);
Obviously there's a common apperance amongst this
There are few corrections in your code. Check the destination folder permission where you are uploading the files. Confirm that folder have writable permission. **IF upload failed, please check with error handler and do the necessary changes such as create upload destination folder, assign file permission etc **
First in your html form add enctype type.
HTML
<form action="../scripts/php/saveupload.php" method="post" enctype="multipart/form-data">
<input name="file" type="file" />
<input name="submit" type="submit" value="Upload File" />
</form>
And then in php file totally 3 changes. I have printed those lines below. Just compare those three changes with your code. In your program the $source_file says $_FILE is undefined. And, I used ftp port 21 in ftp_connect() function. In ftp_put() use 4th variable as FTP_ASCII or FTP_BINARY
$source_file = $_FILES['file']['tmp_name'];
$conn_id = ftp_connect($ftp_server,21);
$upload = ftp_put($conn_id, $destination_file.$_FILES['file']['name'], $source_file,FTP_ASCII);
For full program
PHP
ini_set("display_errors", "1");
error_reporting(E_ALL);
ini_set('display_startup_errors',1);
error_reporting(-1);
print_r(error_get_last());
$ftp_server = "XXXXXX";
$ftp_user_name = "XXXXX";
$ftp_user_pass = "XXXXX";
$destination_file = "/public_html/img/news/";
$source_file = $_FILES['file']['tmp_name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server,21);
ftp_pasv($conn_id, true);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file.$_FILES['file']['name'], $source_file,FTP_ASCII);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
I read many articles and found that username and password should contains only alpha numeric. Also i tried $ftp_server as your localhost.
So to check with the program, try in any internal server (one for both running php file and the same server for ftp file transfering). I know there is no use of same server as FTP for file transfer. But you please try uploading file in same server. If the FTP process works fine, then we will try changing ftp server.
Solved it by using a tutorial on Youtube by Thorn Web... instead of altering what I had above I restarted it and now have the following:
<?php
if($_POST['submit']){
$name = $_FILES['upload']['name'];
$temp = $_FILES['upload']['tmp_name'];
$type = $_FILES['upload']['type'];
$size = $_FILES['upload']['size'];
if(($type == "image/jpeg") || ($type == "image/jpg") || ($type == "image/gif")){
if($size <= 1000000){
move_uploaded_file($temp, "../img/news/$name");
}else{
echo "The file: '<b>$name</b>' is too big...<br>
The size is <b>$size</b> and needs to be less than 100GB. ";
}
}else{
echo "This type '<b>$type</b>' is not allowed";
}
}
?>
This works a treat
Related
I made a script to upload an image to an FTP server but the image is losing quality - and seems like it only has 256 pixels
My HTML:
<form action="upload_image_travel.php" enctype="multipart/form-data" method="post">
<input name="file" type="file" accept="image/jpeg"/>Upload foto in folderul travel/imagini/ pentru ARTICOL
<input name="submit" type="submit" value="Upload File in TRAVEL" />
</form>
And my upload.php:
$ftp_server = "xxxxx";
$ftp_user_name = "xxxxx";
$ftp_user_pass = "xxxxx";
$destination_file = "/public_html/travel/imagini/" . $_FILES['file']['name'];
$source_file = $_FILES['file']['tmp_name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
if (ftp_put($conn_id, $destination_file, $source_file, FTP_ASCII)) {
echo "successfully uploaded $destination_file\n";
header('Location: administrator/admin_index_travel.php');
} else {
echo "There was a problem while uploading $destination_file\n";
}
// close the FTP stream
ftp_close($conn_id);
maybe it should be FTP_BINARY? not ascii?
I have problem with my image uploade, it doesnt work...
give me this error
Connected to ftp.exemple.com, for user exemple#exemple.com Warning:
ftp_put(): Filename cannot be empty in
/home/user/public_html/foto-test.php on line 26 FTP upload has
failed!
<?php
$ftp_server = "ftp.xxxx.com";
$ftp_user_name = "xxxx#xxxx.com";
$ftp_user_pass = "xxxxxxx";
$folder = "/public_html/images";
$destination_file = $folder . $_FILES['file']['name'];
$source_file = $_FILE['file']['tmp_name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); //line 26
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>
I tried to change the $folder destination but it give me the some error...
Check out the answers here PHP FTP upload error: Warning: ftp_put() [function.ftp-put]: Filename cannot be empty in
Also your destination path should have a slash at the end
$folder = "/public_html/images";
$destination_file = $folder . $_FILES['file']['name'];
change that to
$folder = "/public_html/images/";
$destination_file = $folder . $_FILES['file']['name'];
So it will be clear that whatever(image) you uploading is going into the image folder and not 'image+SomeName' folder
I'm trying to make this sort of very simple image-hosting website.
I'm using the code below to upload the images from the input="file" to my ftp directory. The next step is to display the direct image link (eg: www.example.com/123.png) right after uploading on a different page.
<?php
$ftp_server = "myftp.co.uk";
$ftp_user_name = "myusername";
$ftp_user_pass = "mypass";
$destination_file = "/public_html/my/directory/";
$source_file = $_FILES['file']['tmp_name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>
I have no idea how to do this. I checked everywhere and couldn't find a place that explains how to do this.
After i upload any file via ftp_put in php on a server,the file gets uploaded but the file gets corrupted on the server.
following code was used to upload files.
<html>
<body>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
if (isset($_REQUEST['submit'])) {
$local_file = $_FILES["file"]["tmp_name"];
$ftp_path = 'path/newfolder';
$filename = $_FILES["file"]["name"];
$usr = 'userid';
$pwd = 'password';
$host = 'server_ip';
$conn_id = ftp_connect($host, 21) or die("Cannot connect to host");
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
$upload = ftp_put($conn_id, $ftp_path . '/' . $filename, $local_file, FTP_BINARY);
print (!$upload) ? 'Cannot upload' : 'Upload complete';
}
}
?>
I tried jpeg,xls etc...but i cannot open this files after uploading.
Not sure what may cause that issue, but suppose you need to try upload file after move_uploaded_file:
$filename = $_FILES["file"]["name"];
$local_file = "some/tmp/storage/path/$filename";
if(move_uploaded_file($_FILES["file"]["tmp_name"], $local_file)) {
$ftp_path = 'path/newfolder';
$usr = 'userid';
$pwd = 'password';
$host = 'server_ip';
$conn_id = ftp_connect($host, 21) or die("Cannot connect to host");
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
$upload = ftp_put($conn_id, $ftp_path . '/' . $filename, $local_file, FTP_BINARY);
print (!$upload) ? 'Cannot upload' : 'Upload complete';
ulink($local_file); //remove temporary local file
} else {
echo 'File upload error';
}
Also, ensure that file is really uploaded by checking $_FILES["file"]["error"]. It should be equal to UPLOAD_ERR_OK (Other error codes)
I'm using ftp_put() to download a file from another server. Everything (connection etc.) working but it wont download.
ftp_chdir($conn_id, $destination_file);
$upload = ftp_put($conn_id, $name, $source_file, FTP_BINARY);
above is my code and its giving error "Can't change directory to"...
when i out ftp_put without 'ftp_chdir' it didn't work so i used ftp_chdir. but still not working. $destination_file equal to a path and $name equal to a file. Please give me an idea what a i doing wrong?
p.s even $upload is returning true. But i can't find the file anywhere.
Use ftp_get not ftp_put
<?php
// define some variables
$local_file = 'local.zip';
$server_file = 'server.zip';
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}
// close the connection
ftp_close($conn_id);
?>
ftp_get from PHP manual
This is my code,
$source_file="http://example.com/ex/ex2/".$file_name; and $destination_file is equal to a path.
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
//ftp_chdir($conn_id, $destination_file);
$upload = ftp_get($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Downloaded $source_file";
}
// close the FTP stream
ftp_close($conn_id);*/