PHP Forums Archive

curl FTP file Upload

Tags: php, directory, max file size, ftp upload, curl upload

Posted by jcb344 #

Hey I am trying to get a php web to upload to my server via FTP. The example code I found works, exempt it does not save it to any locating. I was wondering if anybody could help me as to how to get the file saved on my server once it has been uploaded to the tmp directory.

my page web page code is:

<form action="curlupload.php" method="post" enctype="multipart/form-data">
<div>
<label for="upload">Select file</label>
<input name="upload" type="file" />
<input type="submit" name="Submit" value="Upload" />
</div>
</form>

And the script is:

<?php
if (isset($_POST['Submit'])) {
 if (!empty($_FILES['upload']['name'])) {
 	$ch = curl_init();
 	$localfile = $_FILES['upload']['tmp_name'];
 	$fp = fopen($localfile, 'r');
 	curl_setopt($ch, CURLOPT_URL, 'ftp://user:password@address.com/'.$_FILES['upload']['name']);
 	curl_setopt($ch, CURLOPT_UPLOAD, 1);
 	curl_setopt($ch, CURLOPT_INFILE, $fp);
 	curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
 	curl_exec ($ch);
 	$error_no = curl_errno($ch);
 	curl_close ($ch);
        if ($error_no == 0) {
        	$error = 'File uploaded succesfully.';
        } else {
        	$error = 'File upload error.';
        }
 } else {
    	$error = 'Please select a file.';
 }
}
echo $error
?>

Thanks

Comments / discussions

Posted by Olaf #

Hi,

just create the directory on your FTP server and modify this row:

curl_setopt($ch, CURLOPT_URL, 'ftp://user:password@address.com/directory1/map2/'.$_FILES['upload']['name']);

the only thing you need is a match in your structure.
check also this tutorial:
http://www.web-development-blog.com/archives/create-custom-backups-from-your-website-using-curl/

Posted by jcb344 #

Thanks, that works great. But now it appears that I am limited upload files under 10M despite my php.ini claiming that I can upload up to 100M. Are there other limits of file size for these commands? My eventual goal is to be able to up load large files (3 GB).

Posted by Olaf #

I use the function for files from a size around 5mb, Don't use it for big files because it's bad for your server. Copy files one by one (check the curl backup tutorial)

Posted by jcb344 #

Well the site that I am working on need to have the ability for users to upload their large movie file (unable to split files) up to the server to be compressed. If this method works, how do I increase the limit (I already changed php.ini to allow for larger files. Is their a better way of uploading large files in php? Thanks

Posted by Olaf #

You need to increase the max post size and also the memory limit and time limit (I don't think that a server can handle these big files... before you send the file to the ftp server you need to process the upload in PHP)

I suggest to create a function that creates FTP accounts and let people upload their files by a regular ftp client. Use a cronjob to process the videos... this would be much safer ;)

Posted by zrox #

thanks a lot ..its working file gets upload!

I want know is the code safe to use on live site?

File gets uploaded directly to remote ftp server? ..no bandwidth used of host?

Posted by Olaf #

Hey zrox,

if you upload a file via a browser there is always bandwidth used. First you upload to the websites host and than the file is uploaded to the ftp location.

You can use this script on a live/public site, but than you need additional validations (file size, type, IP log...)

Posted by zrox #

So is it possible to use another server...

A-server with upload form
B-server with curlupload.php
C-Ftp server

?

Posted by Olaf #

Every form is submitted to the web server which is specified in the forms action attribute.

Sure this can be your server B

Posted by zrox #

Is their any way not expose B-server directly?

Posted by Olaf #

Not really, http headers are mostly traceable, but you can redirect the URL via .htaccess using mod_rewrite.

zrox, this is not related to this topic anymore if you have further questions please open a new thread

Posted by myusername #

Replying am I? Christ knows to what?

Looked up php source, went through your silly register rubbish, didn't want any of your shit except code. Can't find download link.

A main menu that looks like someone ate alphabet soup and threw up.

Maybe when people match your site you should say "waste your life signing up" menu written by a school kid (visually impaired) "can't find a bloody thing" and then you thank me for signing up to something you forced me to, in the first place, just to look for something I can't even find.

Where the hell are the download links where is the software, all I get is a bunch of morons discussing their Sesame Street knowledge.

Thanks for wating my time?

Posted by Attack110 #

ANYWAY!!! I think this is a fantastic idea and it's perfect for what I need, Just a few tweaks needed if you'd kindly give me a hand anyone.

Firstly I've added User name and Password to the form :-

<p align="center"><span class="style3">Username:</span><input name="username" type="text" /></p>
<p align="center"><span class="style3">Password:</span><input name="password" type="password" /></p>

So as you can guess id like the curl to log-in to the FTP with the UN and Pass they supply. something like:-

curl_setopt($ch, CURLOPT_URL, 'ftp://[username]:[password]@myftp.com/'.$_FILES['upload']['name']);

Also me being a pain, would like some info on sticking different security ideas to it, like file types and so on. but that's for another day I think

Thanks in advance people.

Posted by jdserran #

hi, did you make it work? i'm talking about uploading really large files. i'm in a similar problem and my client has no admin privileges on the server to change things like the max_post_size, etc. i'm using curl ftp 'coz ftp supposedly has no upload limit but when i try to upload a 170MB-size file, it doesn't get uploaded and my success/failure messages don't get fired up.

Quote from: zrox
"thanks a lot ..its working file gets upload!
I want know is the code safe to use on live site?
File gets uploaded directly to remote ftp server? ..no bandwidth used of host?"

Posted by Olaf #

Hi,
the server limit is your problem. Because you're using cURL/FTP function will not say that a file is not uploaded first. Only the file handling is done with cURL. If you really need a large file upload function, get some real hosting account ;)

Posted by jdserran #

Really? that's not good. i wonder, how come if uploading is through ftp client, there's no upload limit?

Quote from: Olaf
"Hi,
the server limit is your problem. Because you're using cURL/FTP function will not say that a file is not uploaded first. Only the file handling is done with cURL. If you really need a large file upload function, get some real hosting account ;)"

Posted by Olaf #

this is because the FTP upload is done between an ftp client and a ftp server.
The upload via websites is between the browser and the (php enabled) web server. After upload the file is transferred second time to the ftp server. The ftp / curl function is to upload to a ftp server on a different server or network.

Posted by jdserran #

So because i'm using curl-ftp to upload to the same server, server won't allow it (large file), but if i'm uploading to a different server or network, it will do it?

Quote from: Olaf
"The ftp / curl function is to upload to a ftp server on a different server or network."

Posted by Olaf #

No, the limit is related to the site where the upload function is hosted. If you upload via the browser, a curl ftp function is not needed.

But first you need to raise the max upload size or the user need to use a FTP client (but I guess that is not what you want)

It's not possible to upload via a regular website and bypass the limit.