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']));?>">
Related
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
I am currently developing a website where users can upload and share there websites.
I have tried to create a upload feature but failed every attempt.
I just want a file upload system, preferably created with PHP, that will upload a visitors HTML page and will automatically put the link of there page somewhere on the index.html page.
Here is the code for index.html:
<!DOCTYPE html>
<html>
<body>
<style>
* {
font-family: sans-serif;
}
</style>
<h1>File Upload</h1>
<form action="upload.php" enctype="multipart/form-data" method="POST">
<input type="file" name="html"> <br/> <br/>
<input type="submit" value="Upload">
</form>
</body>
<br/> <br/>
<body>
Example Link <!-- Link of Uploaded file -->
</body>
</html>
Here is the code for upload.php:
<?php
// Check if the form was submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_FILES["html"]) && $_FILES["html"]["error"] == 0){
$allowed = array("html", "htm");
$filename = $_FILES["html"]["name"];
$filetype = $_FILES["html"]["type"];
$filesize = $_FILES["html"]["size"];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($ext, $allowed)) die("Sorry, the file you selected is not supported");
$mazsize = 40MB;
if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
if(in_array($filetype, $allowed)){
if(file_exists("upload/" . $filename)){
echo $filename . " is already exists.";
} else{
move_uploaded_file($_FILES["html"]["tmp_name"], "upload/" . $filename);
echo "Success!";
}
} else{
echo "An error occured while uploading your file.";
}
} else{
echo "Error: " . $_FILES["html"]["error"];
}
}
?>
Please help.
You're asking a bit odd questions to say the least. Your code is not consistent with your questions or request either. Automatically upload HTML? And yet you have a form to store user website HTML?
Well user will have to provide you with their website HTML and also their website URL. Which means you need to adjust your script to handle both options. Yet I only see one option, to upload HTML. And there is nothing automatic about it either. It's just a simple upload text function. If you want a real automatic version? Then you should think outside the box. You could for example let users provide their URL and then where you make some sort of a bot to go and take screenshots of that website layout. Also using a simple JS you could add the option to write user website url. Then below an iframe will be undated loading user url. Then you could use https://html2canvas.hertzen.com/ version 1.0.0-rc.1 to make an approximate snapshot of user url layout.
Who otherwise in their right mind would upload their entire HMTL? For that you need a lot more work than what you've provided. A website is not only HMTL. Without JavaScript these days there is no website. Because almost every website layout will break down without JavaScript. Then there are images without the full url since images are usually loaded locally or even trough AWS or something.
Anyways, you need to go back to the drawing board and figure out what exactly you need. I would suggest that you need people to upload an archive with the entire website if that is what you need, an entire website?
But if you ask wrong questions, then you will receive wrong answers. There is no way in the world this code you've provided will be sufficient for anything useful other than some testing purposes.
I have Updated the code and now you can try it
You have used file size in in proper way
And it cause errors
<?php
// Check if the form was submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_FILES["html"]) && $_FILES["html"]["error"] == 0){
$allowed = array("html", "htm");
$filename = $_FILES["html"]["name"];
$filetype = $_FILES["html"]["type"];
$filesize = $_FILES["html"]["size"];
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if(!in_array($ext, $allowed)) die("Sorry, the file you selected is not supported");
if($filesize > 4000000) die("Error: File size is larger than the allowed limit.");
if(file_exists("upload/" . $filename)){
echo $filename . " is already exists.";
} else{
if(move_uploaded_file($_FILES["html"]["tmp_name"], "upload/" . $filename)){
echo "Success!";
} else{
echo "An error occured while uploading your file.";
}
}
} else{
echo "Error: " . $_FILES["html"]["error"];
}
}
?>
<!DOCTYPE html>
<html>
<body>
<style>
* {
font-family: sans-serif;
}
</style>
<h1>File Upload</h1>
<form action="upload.php" enctype="multipart/form-data" method="POST">
<input type="file" name="html"> <br/> <br/>
<input type="submit" value="Upload">
</form>
</body>
<br/> <br/>
<body>
Example Link <!-- Link of Uploaded file -->
</body>
</html>
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.
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')");
I write the php to upload file and scan the directory to show them as links, the scaning directoy works well I can see the text files I created in the directory, but I just can not move the local file to the desired directory .No file shows up after execution.
I think the problem may contain in this line:
move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/BlueTapeLogin/upload".$_FILES["file"]["name"]);
What I really want is to upload the image to the directory in /var/www/BlueTapeLogin/upload
and my php file lives in /var/www/BlueTapeLogin/upload_image.php
How can I change the code to make things work? Thanks in advance.
Please see my full code:
<html>
<head>
<?php
try
{
if (!empty($_POST["delete"])){
$delete=$_POST["delete"];
echo"we have the command delete this file:";
echo $delete;
$file = "upload/".$delete;
echo "/n***************";
echo "you want delete :";
echo $file;
echo "***************";
if (!unlink($file))
{
echo ("Error deleting $file");
}
else
{
echo ("Deleted $file");
}
}else{}
}catch(Exception $e)
{
echo 'Message: ' .$e->getMessage();
}
?>
<?php
move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/BlueTapeLogin/upload".$_FILES["file"]["name"]);
?>
<?php
$dir=scandir("/var/www/BlueTapeLogin/upload") ;
for($j=0;$j<count($dir);$j++){
echo $dir[$j];
echo"\n";
$target = $dir[$j]; // This is the file that already exists
$link = $dir[$j]; // This the filename that you want to link it to
echo "".$link."";
}
?>
</head>
<body>
<form action="upload_image.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"><br>
<label for="file">Delete</label>
<input type="text" name="delete" id="delete"><br>
<input type="submit" name="submit" value="Submit">
</form>
logout
</body>
</html>
You're missing a directory separator between upload and the filename, it should be:
move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/BlueTapeLogin/upload/".$_FILES["file"]["name"]);
The permissions you show say that only root can write into that directory, and but the webserver is probably using a userid like www-user. You need to change the ownership of the directory to the webserver userid. This will have to be done by the server administrator.
More likely, there's another directory that the webserver is already allowed to write into. The server administrator should be able to tell you what directory to use.
Check user permission to upload in that directory and try to make it simple. Just simply upload a file to other directory see it is working or not than check your code go one by one step.
Check return values check whether a warning is issued
Return Values
Returns TRUE on success.
If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.
If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.