Upload file from php to ftp server [duplicate] - php

This question already has answers here:
PHP ftp_put fails
(2 answers)
Closed 18 days ago.
I'm using a script to upload file via php script to my ftp server.
The connection is ok but every time i try to upload files it says:
Error uploading file! Please try again later.
so it seems that there is something wrong in the script.
Directory destination is mydomain/test/upload/
and the script is into: mydomain/test/index.php
Anyone of you can help me to understand?
// ftp settings
$ftp_hostname = 'xxxx'; // change this
$ftp_username = 'xxxx'; // change this
$ftp_password = 'xxxx'; // change this
$remote_dir = '/upload/'; // change this
$src_file = $_FILES['srcfile']['name'];
if ($src_file!='')
{
// remote file path
$dst_file = $remote_dir . $src_file;
// connect ftp
$ftpcon = ftp_connect($ftp_hostname) or die('Error connecting to ftp server...');
// ftp login
$ftplogin = ftp_login($ftpcon, $ftp_username, $ftp_password);
// ftp upload
if (ftp_put($ftpcon, $dst_file, $src_file, FTP_ASCII))
echo 'File uploaded successfully to FTP server!';
else
echo 'Error uploading file! Please try again later.';
// close ftp stream
ftp_close($ftpcon);
}
else
header('Location: index.php');
and the index file is:
<!DOCTYPE html>
<html>
<head>
<title>Upload Files to FTP Server in PHP</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" >
<link href="css/bootstrap.css" rel="stylesheet" type="text/css" />
</head>
<body>
<br/>
<div class="container">
<div class="col-xs-8 col-xs-offset-2 well" style="background:none;">
<form action="ftp_upload.php" method="post" enctype="multipart/form-data">
<legend>Please Choose File to Upload</legend>
<div class="form-group">
<input type="file" name="srcfile" />
</div>
<div class="form-group">
<input type="submit" name="submit" value="Upload File to FTP Server" class="btn btn-warning"/>
</div>
</form>
</div>
</div>
</body>
</html>
I want to have uploaded file into the upload folder:
mydomain/test/upload/

Either the source file is incorrect, or your FTP sits behind a firewall.
If it's behind a firewall, try this ftp-pasv

Related

Displaying recently uploaded images from remote FTP server in PHP

Recently, I have created an upload form in which users can upload their files to a remote FTP server. Until now, everything is going well. However, I have a problem.
I want to make sure that when the user his image is uploaded to the remote FTP server, the image will be displayed immediately on the website. How can I do this? This question has been asked a lot on Stack Overflow. Yet there is a difference. In most cases, the individual wanted to download a specific file from the remote FTP server. This is not the case with me. I want to make sure that the user sees the file he uploaded displayed on the site.
My php code for uploading a file to the remote FTP server:
<?php
if ( empty( $_FILES['file'] ) ) {
?>
<html>
<head>
</head>
<body>
<form action="" enctype="multipart/form-data" method="post">
<input name="file" type="file"/>
<br>
<input name="submit" type="submit" value="Upload uw album" />
</form>
</body>
</html>
<?php
return;
} else {
?>
<html>
<head>
</head>
<body>
<form action="" enctype="multipart/form-data" method="post">
<input name="file" type="file"/>
<br>
<input name="submit" type="submit" value="Upload uw album" />
</form>
</body>
</html>
<?php
}
$ftp_server = "myserver";
$ftp_user_name = "myuser";
$ftp_user_pass = "mypass";
$source_file = $_FILES['file']['tmp_name'];
$destination_folder = "/public_html/wp/wp-content/plugins/AbonneerProgrammas/Albums";
$destination_file = $destination_folder . "/" . basename($_FILES['file']['name']);
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "Het spijt ons, er is momenteel geen connectie met de server.";
//echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
//echo "upload is gelukt";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
//
} else {
?>
<meta http-equiv="refresh" content="0; url=https://radioprogrammabank.nl/wp/upload-album/?name=<?php echo urlencode(basename($_FILES['file']['name']));?>">
<?php
echo "".htmlspecialchars($source_file)."";
header('Content-Type: application/octet-stream');
echo file_get_contents('ftp://username:password#ftp.example.com/path/' . $_GET["file"]);
echo "upload is gelukt";
}
// close the FTP stream
ftp_close($conn_id);
?>
Once the FTP upload succeeds, you need to redirect user's browser to a viewer page.
header("Location: view.php?name=".urlencode(basename($_FILES['file']['name'])));
In the view.php, use the code from your examples to "download a specific file from the remote FTP server". Or see List and download clicked file from FTP.
Note that for the Location header to work, you cannot output anything before – so no echo and no HTML code.
But as you do not have a standalone PHP code, but you executed it from within some WordPress plugin, the WordPress will have already outputted some HTML headers before your code even starts. So the redirect won't work.
There's a question on this on the WordPress development Stack Exchange:
wp_redirect() not working in Insert PHP plugin in WordPress.
And you have asked another one:
Error: “Cannot modify header information”
Or as a lame hack, you can try meta redirect:
<meta http-equiv="refresh" content="0; url=view.php?name=<?php echo urlencode(basename($_FILES['file']['name']));?>">

move_uploaded_file upload function fails in AWS EC2 Instance with message "failed to open stream: Permission denied"

I have a simplest php page for just uploading only MS Word or pdf files by a user. The function "move_uploaded_file" simply DOES not work. I have checked the limits in php.ini, checked with file and folder permissions in the target folder everything googled and verified, including to see if SELinux has any issues but it simply refuses to upload and neither is there any error seen as well. Frustrated now and nowhere replies related to EC2 instance are mentioning the solution online. Request experts to help and understand what is going wrong in the code presented below
Mine is a T2 large instance with PHP 7.2.11, Apache/2.4.37 (Red Hat Enterprise Linux), mysqld 8.0.17, still not understanding what is the issue, and no error is logged in error.log as well
<?php
include 'includes/UploadException.php';
define ('SITE_ROOT', realpath(dirname(__FILE__)));
echo "Current User:".get_current_user()."<br />";
if ($_POST['hdnOp'] == "A"){
$file = $_FILES['resume'];
$filename = $_FILES['resume']['name'];
$fileTmpName = $_FILES['resume']['tmp_name'];
$filesize = $_FILES['resume']['size'];
$fileerror = $FILES['resume']['error'];
$filetype = $_FILES['resume']['type'];
if ($filetype=="application/msword" || $filetype=="application/pdf"){
$random = rand(1111,9999);
$newfilename = $random.$filename;
$uploadpath = "/BMT/UserProfiles/".$newfilename;
echo "<h2>FILE = ".$newfilename."</h2>";
echo "<h2>ULPATH = ".$uploadpath."</h2>";
move_uploaded_file($fileTmpName,$uploadpath);
if ($_FILES['resume']['error'] === 0){
echo "<h3>ALL OK</h3>";
echo "ERROR CODE = ".$_FILES['resume']['error']."<br />";
echo "File Name :".$newfilename;
echo "File Size :".$filesize." kb";
echo "File Type :".$filetype;
} else {
throw new UploadException($_FILES['resume']['error']);
}
}
}
?>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=yes">
<!-- Bootstrap CSS -->
<!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<title>UPLOAD</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
function submitForm(this){
return true;
}
</script>
</head>
<body class="container">
<table class='table table-bordered table-hover table-sm'><body>
<tr><td>
<form id="teacher-form" class="login-form" name="teacher-form" method="POST" enctype="multipart/form-data" action="test.php" onsubmit="return submitForm(this)">
<input type='hidden' name='hdnOp' value='A'>
<div id="form-content"></td></div><td></td></tr>
<tr><td>
<label>Upload Resume<b>*</b> <i>Note: Most recent upload will overwrite earlier version (if any)</i></label><br />
<input type="file" name="resume" id="resume" accept=".pdf,.docx,.doc,.rtf"><br /><div id='errresume'></div>
</td></tr>
<tr><td><input name="submit" type="submit" value="SUBMIT" /></td></tr>
</table>
</div>
</form>
</body>
</html>
I should be able to see the file in /BMT/UserProfiles/ folder
[root#ip-XXX-XX-XX-XXX bmt]# ls -al /BMT/UserProfiles/
total 4
drw-r--r--. 2 apache apache 24 Oct 16 12:17 .
drw-r--r--. 3 apache apache 26 Oct 16 12:05 ..
ADyson finally managed to get the solution to this problem....
chcon -R --type httpd_sys_rw_content_t /BMT/UserProfiles/
Then stop and start the httpd service Had a doubt if somewhere SELinux was responsible... after this now its working fine. Thanks for all the help and suggestions from your end, really helped me to debug from different perspective

Large file upload issue with PHP and FTP

i am having an issue with a component of a website i am designing. i am having trouble uploading files through the page... PHP is capped at 32mb. the file system i am working on, i expect files in the neighbourhood of 500mb. Most around 250-300... but want that buffer. i have heard of direct ftp uploads through. I do believe this is the direction i need to go in:
<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "localfile.txt";
// upload file
if (ftp_put($ftp_conn, "serverfile.txt", $file, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
?>
this would be my html and php.
<?php
include_once(db_conx.php);
?><?php
error_reporting(E_ALL);
if(isset($_POST['submit'])){
$name = $_FILES['file']['name'];
$temp = $_FILES['file']['tmp_name'];
move_uploaded_file($temp,"vids/".$file);
$url = "http://x-webb.com/vids/.$file";
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>video uploader</title>
<meta name="" content="">
</head>
<body>
<h1>Video Uploader</h1>
<form method="POST" enctype="multipart/form-data">
Video: <br>
<input type="file" name="name"><br />
<label for="title">Title:</label><br>
<input type="text" name="title" id="title" placeholder="required" ><br>
<label for="description">Description:</label><br>
<textarea name="description" id="description" placeholder="required" ></textarea><br>
<label for="tags">Tags:</label><br>
<textarea name="tags" id="tags" placeholder="required" ></textarea><br>
<input type="submit" value="upload">
</form>
</body>
</html>
i just cant seem to put the 2 together. I have the ftp_conx.php file for the ftp server..checked out ok... no errors.
i have been programing for a hobby for about 18 months with html and CSS... about 2 months with ajax and php. my built pages are
autodude666.com/network
x-webb.com
current project, where i wish to place this code, is :
http://x-webb.com
ANY help would be GREATLY appreciated. TY in advance.
You can increase the maximum upload size in PHP. Adjust in php.ini:
upload_max_filesize = 500M
post_max_size = 500M
Additionally, you should consider the amount of time it takes a user to upload a 500MB file and set the maximum time limit accordingly.
I see that you're using Apache from the server signature header of the site you provided, but for anyone else who might be running nginx + php-fpm, you also have to increase the value of client_max_body_size in the http block of your nginx config or you'll see 413 errors.

php form works fine on localhost, but send empty inputs online

I have some forms in php that upload data into a mysql database. I have already tested on a localhost server with xampp, but when I upload my files into my host ftp, the forms do not work. I think it has something to do with the fact that I put this file and also the "connect.php" file under password restriction, and that has something to do with the file permissions. I've already granted permission to the user of the database and permission to the file and I also had tried without the password protection, but the result is the same.
So I start to experiment a little and I figured out that the image uploads fine into the database but the other inputs don't, neither the "header" input nor the "article" textarea... knowing that, I tried to "echo out" these input results but none of them show something unless the image input. I came to the conclusion that my form is sending empty data. Can someone make suggestions as to what is happening and how to fix it? Here's my code:
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<div id="slider_upload">
<h1>Slider principal</h1>
<div class="forma">
<form action="sliderPrincipal.php" method="post" enctype="multipart/form-data">
<p><label for="header">Header</label>
<input type="text" id="header" name="header" /></p><br>
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
<p><label for="fileupload">File to upload</label>
<input type="file" id="fileupload" name="fileupload" /></p><br>
<p><label for="article">article</label>
<textarea id="article" name="article" rows="26" style="width: 100%;" >Write here</textarea></p><br>
<button type="submit" name="submit" value"send">Upload File</button>
</form><br><br>
</div>
</div>
<div class="message">
<?php
$file_dir = "../uploaded";
if (isset($_POST['header'])){
$header = mysql_real_escape_string($_POST['header']);
$article = mysql_real_escape_string($_POST['article']);
}
foreach($_FILES as $file_name => $file_array){
//echo "path: ".$file_array['tmp_name']."<br />\n";
//echo "name: ".$file_array['name']."<br/>\n";
//echo "type: ".$file_array['type']."<br/>\n";
//echo "size: ".$file_array['size']."<br/><br/>\n";
//echo "encabezado ".$encabezado;
if(is_uploaded_file($file_array['tmp_name'])){
move_uploaded_file($file_array['tmp_name'], "$file_dir/".$file_array['name']) or die ("Your image couldnt be uploaded <br>");
$newpath = addslashes("uploaded/".$file_array['name']);
include "connect.php";
$addfile = "INSERT INTO slider (header, image, article) VALUES ('$header','$newpath', '$article')";
$result = mysql_query($addfile);
if ( $result === FALSE ) {
echo "your post could not be uploaded but we already got your image in our files ";
} else{
echo '<p style="padding: 20px;"><h1>Your post was successfully uploaded <h2></p><br><br>';
}
mysql_close();
} else "No file found";
}
?>
</div>
</div>
</body>
</html>

File Upload php

So I am trying to upload files to the database through php here is the code
HTML:
<!doctype html>
<html>
<head>
<title>Image Upload</title>
</head>
<body>
<form action="uploadimage.php" method="POST" enctype="multipart/form-data">
<input type="text" name="tag"/>
<input type="file" name="file"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
php:
<?php
$target = "files/";
$target = $target . basename( $_FILES['file']['name']);
$name=$_POST['tag'];
$file=($_FILES['file']['name']);
mysql_connect("localhost","Bluesir9","Bluesir9","website");
mysql_query("Insert into files values('$name','$file');");
if(move_uploaded_file($_FILES['file']['tmp_name'],$target))
{
echo "The File ".basename($_FILES['file']['name'])."has been uploaded";
}
else
{
echo "Sorry there was an error";
}
?>
when I check the target folder the files are there, but I cant find the files on the database returns an empty result set when I query it through phpMyAdmin.
What am I doing wrong?
Since the target files are there in folder, and if you can't find the files on the database then obvious thing should be to check insert query.
Please check syntax of insert query
mysql_query("Insert into files values('$name','$file')");

Categories