Jump to content

PHP form to upload via FTP


Rhythmdvl

Recommended Posts

Hello,

 

I need to set up a file sharing area on my Web page so clients can up/download materials (50–80 MB Quark and other docs). I thought I could do it with move_uploaded_file, but it turns out my host limits PHP-script transfers to just 5 MB. However, they tell me I can use my FTP connection to transfer up to 2 GB.

 

Is there a workaround? I’d rather not go the straight FTP route for a variety of reasons. I see that there are several ftp_ commands I can use in a PHP script, but I don’t know if they will only work on files that have already been uploaded. That is, if move_uploaded_file is going to fail, will ftp_put ever get a chance to work?

 

Or is there some way to build a browse button that will use the local file location to send the file via FTP?

 

Clearly I’m not very familiar with PHP, so please forgive me if this is an obvious question.

 

Thanks,

 

Rhythm

 

Here is what I’ve been toying with so far, but I’m getting a “failed to open stream: No such file or directory” error on the ftp_put line. (Again, I’m fairly new so this may not be very elegant.)

 

<?
// this should go in a connect file
$HOST="192.168.1.102";
$UN="user";
$PW="pass";
$DIR="/sandbox/documents/";
// this should go in a connect file

if (isset($_POST['Submit'])) 
{
$FILE = $_FILES['uploaded']['name'];
echo $FILE; 

$conn = ftp_connect($HOST);
if(!$conn) {
   exit("Could not connect to server: $HOST\n");
    }

    if(!ftp_login($conn,$UN,$PW)) {

   ftp_quit($conn);
   exit("Could not log in\n");
    }

    ftp_chdir($conn,$DIR);


ftp_put($conn,$DIR.$FILE,$FILE,FTP_ASCII);


ftp_quit($conn);




{


} } 

else 

{
echo "this is not yet submitted";
}

// $target = "documents/";
// $target = $target . basename( $_FILES['uploaded']['name']) ;
//echo $_FILES['uploaded']['name'];

// PHP script to read the directory START
function getFileList($dir) 
{ # array to hold return value 
$retval = array(); 

# add trailing slash if missing 
if(substr($dir, -1) != "/") $dir .= "/"; 

# open pointer to directory and read list of files 
$d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading"); 
while(false !== ($entry = $d->read())) { 

	# skip hidden files 
if($entry[0] == ".") continue; 
if(is_dir("$dir$entry")) { 
$retval[] = array( 
	"name" => "$dir$entry/", 
	"type" => filetype("$dir$entry"), 
	"size" => 0,
	"lastmod" => filemtime("$dir$entry") 
	); 
			 sort($retval);

} elseif(is_readable("$dir$entry")) {
	 $retval[] = array( 
	 "name" => "$entry", 
	 "type" => mime_content_type("$dir$entry"),
	 "size" => filesize("$dir$entry"),
	 "lastmod" => filemtime("$dir$entry") 
	 ); 
	 sort($retval);
	 }
	  }
	   $d->close(); 
	   return $retval; 
	   }
	   
//call the listing function 
$dirlist = getFileList("documents/");


echo "<table>\n"; 
echo "<tr><th>Name</th><th>~Size (MB)</th></tr>\n"; 

foreach($dirlist as $file) { 
echo "<tr>\n"; 
//echo "<td>{$file['name']}</td>\n";
echo ("<td><a href='documents/{$file['name']}'>{$file['name']}</a></td>\n" );
//echo "<td>{$file['type']}</td>\n"; 
//echo "<td>{$file['size']}</td>\n"; 
	$f_size = $file['size'];
	$round_f_size = round($f_size/pow(1024, 2), 2);
	echo "<td> $round_f_size <td>\n";
echo "<td>" . date("r", $file['lastmod']) . "</td>\n"; 
echo "</tr>\n"; 
} 
echo "</table>\n\n";

    




// PHP script to read the directory END



?>

<p> </p>
<p> </p>
<p> </p>
<p> </p>
<form action="phpftp4.php" method="post" enctype="multipart/form-data" name="form1">
  <input type="submit" name="Submit" value="Submit">
  <input name="uploaded" type="file" size="100">

  
</form>

 

 

Link to comment
https://forums.phpfreaks.com/topic/127148-php-form-to-upload-via-ftp/
Share on other sites

You can try to change the limit via the ini_set function, but I don't know if your hosting company will allow that, you'll just have to try it :D

 

And

 

$HOST="192.168.1.102";

 

is certally not a valid ip. That is the ip given to you by your router or something not the ftp site or your website's ip.

Thanks for the suggestion. I'll give it a whirl (and hope if they notice they're not too upset!)

 

That IP address in my code is the local testing server address. I built a Linux box (Ubuntu) to use as a file server here in the home office (wife and I work on two different platforms but on many of the same files). I also installed Apache and an FTP server (among other things), so I can use it as an occasional page tester. When working in Dreamweaver, the site definition has the remote (hosted) location and the local (testing) server -- it really speeds things up. When I'm trying to figure something out, F12 (test) is instantaneous, especially compared to testing on the remote host. When all is ready, I just put the file (via Dreamweaver) to the remote host, and re-check to make sure there are no differences/problems.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.