note 72976 rejected from ref.curl by tularis
| From: | tularis@php.net | Date: | Tue, 06 Feb 2007 16:48:08 +0000 |
| Subject: | note 72976 rejected from ref.curl by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-122930@lists.php.net to get a copy of this message | ||
Note Submitter: shawnhausser at yahoo dot com
----
I am having a hard time finding a way to display a progress bar for CURL on PHP. I am a little
unfamiliar with CURL but there must be an easy implementation to have some kind of progress meter
display on the user's browser about the FTP upload.
<?
if($_POST['submit']) {
$server = 'ftp://www.signsrus.info';
$u_name = 'user';
$p_word = 'pass';
$up_file = $HTTP_POST_FILES['file']['tmp_name'];
$srvr_path = '/uploads/';
$new_name = $HTTP_POST_FILES['file']['name'];
// Append path & uploaded file name to server name, pass with CURLOPT_URL.
$url = $server . $srvr_path . $new_name;
// fopen() upload file for handle to pass with CURLOPT_INFILE.
$fp = fopen($up_file, 'r');
if (!$fp) {
echo 'Failed to open upload file.' . '<br />';
exit;
}
if (!($ch = curl_init())) {
echo 'Unable to allocate resource.' . '<br />';
exit;
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $u_name . ':' . $p_word);
// If true w/CURLOPT_RETURNTRANSFER server output returned by curl_exec().
// (Usually no output w/FTP, anyway.)
// If false, curl_exec() will return true on success.
// Either way curl_exec() returns false on failure.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_UPLOAD, true);
curl_setopt($ch, CURLOPT_INFILE, $fp);
// Show progress
curl_setopt($ch, CURLOPT_PROGRESS, 1);
// Pass true w/CURLOPT_TRANSFERTEXT for ascii, false for binary.
curl_setopt($ch, CURLOPT_TRANSFERTEXT, true);
// File size w/CURLOPT_INFILESIZE must be accurate for clean transfer.
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($up_file));
// Everything's ready, now curl_exec() does it all.
$curl_return = curl_exec($ch);
if ($curl_return === false) {
// Show failure info.
echo 'curl_exec() failed.' . '<br />';
echo 'curl_errno() = ' . curl_errno($ch) . '<br />';
echo 'curl_error() = ' . curl_error($ch) . '<br />';
} else {
// Show success info.
echo 'File Transferred Successfully.' . '<br />';
echo '<br /><b>curl_getinfo()</b>' . '<br />';
foreach (curl_getinfo($ch) as $key => $value) {
echo $tab . $key . ' => ' . $value . '<br />';
}
}
curl_close($ch);
}
?>
<html>
<head>
</head>
<body>
<form name="upload" method="post" action="upload.php"
enctype="multipart/form-data">
<input type="file" name="file">
<br><br>
<input type=submit value="Submit" name="submit" />
</form>
</body>
</html>