Im trying to upload a mp3 file to my ftp server:
$data = substr($_POST['data'], strpos($_POST['data'], ",") + 1);
$decodedData = base64_decode($data);
$filename = urldecode($_POST['fname']);
$cid = ftp_connect("foo.com");
$result = ftp_login($cid, "rodrigo#foo.com","password");
if ((!$cid) || (!$result)) {
echo "connection failed"; die;
} else {
echo "connected";
}
ftp_pasv ($cid, true);
ftp_chdir($cid, "my_folder");
if (ftp_put($cid, $filename, $decodedData, FTP_BINARY)) {
//...
} else {
//...
}
I have this warning:
Warning: ftp_put(���) [function.ftp-put]: failed to open stream: Invalid argument in...
I cant find out how to send a valid argument
You will need to create a file that you can pass, the below example writes the file to memory instead of a file on your disk ... but you may want to write it to disk depending on the size.
$tmp = fopen('php://memory', 'r+');
fputs($tmp, $decodedData);
rewind($tmp);
if (ftp_fput($cid, $filename, $tmp, FTP_BINARY)) {
}
Related
<?php
// FTP access parameters
$source_ftp_server = 'ftp://uk9.siteground.eu';
$source_ftp_user_name = 'xyz';
$source_ftp_user_pass = 'abc';
// file to move:
$local_file = '/Users/Swayam/Desktop/Test.csv';
$remote_file = '/home/c10mill2/public_html/edtopiadb/Test.csv';
$fp = fopen($local_file, 'r');
$conn_id = ftp_connect($source_ftp_server);
$login_result = ftp_login($conn_id, $source_ftp_user_name, $source_ftp_user_pass);
$ret = ftp_nb_fput($conn_id, $remote_file, $fp, FTP_BINARY);
while ($ret == FTP_MOREDATA) {
// Establish a new connection to FTP server
if(!isset($conn_id2)) {
$conn_id2 = ftp_connect($source_ftp_server);
$login_result2 = ftp_login($conn_id2, $source_ftp_user_name, $source_ftp_user_pass);
}
// Retreive size of uploaded file.
if(isset($conn_id2)) {
clearstatcache(); // <- this must be included!!
$remote_file_size = ftp_size($conn_id2, $remote_file);
}
// Calculate upload progress
$local_file_size = filesize($local_file);
if (isset($remote_file_size) && $remote_file_size > 0 ){
$i = ($remote_file_size/$local_file_size)*100;
printf("%d%% uploaded<br>", $i);
flush();
}
$ret = ftp_nb_continue($conn_id);
}
if ($ret != FTP_FINISHED) {
echo "<span style='color:red;'><b>There was an error uploading the file...</b></span><br>";
exit(1);
}
else {
echo "<br>Files successfully uploaded!<br><br>";
}
fclose($fp);
Pls help me with the code to get it working.
When i execute this code i get a error -
Warning: ftp_connect(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known in /Users/Swayam/Desktop/sample.php on line 14
Warning: ftp_login() expects parameter 1 to be resource, boolean given in /Users/Swayam/Desktop/sample.php on line 15
Warning: ftp_nb_fput() expects parameter 1 to be resource, boolean given in /Users/Swayam/Desktop/sample.php on line 16
<span style='color:red;'><b>There was an error uploading the file...</b></span><br>
i've a text file on 000webhost server with url [http://file_server.comxa.com/remote_server/temp.txt][1]
i want to read first value till delimiter,here is the code
<?php
include 'dbconfig.php';
$file = fopen ("http://file_server.comxa.com/remote_server/temp.txt", "r");
if (!$file) {
echo "<p>Unable to open remote file.\n";
exit;
}
while (!feof ($file)) {
$line = fgets ($file, 1024);
if (preg_match ("#\<title\>(.*)\</title\>#i", $line, $out)) {
$title = $out[1];
break;
}
$sql = "INSERT INTO `temprature` (`id`, `temprature`) VALUES (NULL, $line)";
$result = mysqli_query($conn,$sql) or die(mysqli_error($conn));
if($result)
{
echo "temprature inserted";
}
else
{
echo "failure";
}
}
fclose($file);
?>
but this code is throwing error
Warning: fopen(http://file_server.comxa.com/remote_server/temp.txt): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in C:\xampp\htdocs\demo\server_read.php on line 3
Unable to open remote file.
Let me know where i'm wrong
P.S: its working fine when i'm changing path to my localhost file
I do fetch a file with
$fp = fopen('uploads/Team/img/'.$team_id.'.png', "rb");
$str = stream_get_contents($fp);
fclose($fp);
and then the method gives it back as image. But when fopen() fails, because the file did not exists, it throws an error:
[{"message":"Warning: fopen(uploads\/Team\/img\/1.png): failed to open stream: No such file or directory in C:\...
This is coming back as json, obviously.
The Question is now: How can i catch the error and prevent the method from throwing this error directly to the client?
You should first test the existence of a file by file_exists().
try
{
$fileName = 'uploads/Team/img/'.$team_id.'.png';
if ( !file_exists($fileName) ) {
throw new Exception('File not found.');
}
$fp = fopen($fileName, "rb");
if ( !$fp ) {
throw new Exception('File open failed.');
}
$str = stream_get_contents($fp);
fclose($fp);
// send success JSON
} catch ( Exception $e ) {
// send error message if you can
}
or simple solution without exceptions:
$fileName = 'uploads/Team/img/'.$team_id.'.png';
if ( file_exists($fileName) && ($fp = fopen($fileName, "rb"))!==false ) {
$str = stream_get_contents($fp);
fclose($fp);
// send success JSON
}
else
{
// send error message if you can
}
You can use the file_exists() function before calling fopen().
if(file_exists('uploads/Team/img/'.$team_id.'.png')
{
$fp = fopen('uploads/Team/img/'.$team_id.'.png', "rb");
$str = stream_get_contents($fp);
fclose($fp);
}
[{"message":"Warning: fopen(uploads\/Team\/img\/1.png): failed to open stream: No such file or directory in C:\...
the error is clear: you've put the wrong directory, you can try what you whant but it'll not work. you can make it work with this:
take your file and put it in the same folder of your php file
(you'll be able to move it after don't worry, it's about your error)
or on a folder "higher" of your script (just not outside of your www
folder)
change the fopen to ('./$team_id.'png',"rb");
rerun your script file
don't forget this : you can't access a file that is'nt in your "www" folder
(he doesn't found your file because he give you her name: the name come from the $team_id variable)
Generically - This is probably the best way to do file-io in php (as mentioned by #Cendak here)
$fileName = 'uploads/Team/img/'.$team_id.'.png';
if ( file_exists($fileName) && ($fp = fopen($fileName, "rb"))!==false ){
$str = stream_get_contents($fp);
fclose($fp);
// send success JSON
}else{
// send an error message if you can
}
But it does not work with PHP 7.3, these modifications do,
if(file_exists($filename) && ($fp = fopen($filename,"r") !== false)){
$fp = fopen($filename,"r");
$filedata = fread($fp,filesize($filename));
fclose($fp);
}else{
$filedata = "default-string";
}
I am trying to retrieve and stream a file to the browser from an ftp site.
The error is:
Warning: ftp_nb_get() expects parameter 2 to be string, resource given
I know that it is a resource but how do I get around this?
if(isset($_GET['filename'])){
$requestfilename = $_GET['filename'];
if($sockets = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP)){
stream_set_write_buffer($sockets[0], 0);
stream_set_timeout($sockets[1], 0);
if($ftp_connection = ftp_connect($ftp_server)){
if (#ftp_login($ftp_connection, $ftp_user, $ftp_pass)) {
if(#ftp_chdir($ftp_connection, $ftp_dir_new)){
if($ret = ftp_nb_get($ftp_connection, $sockets[0], $requestfilename, FTP_BINARY)){
while(ftp_nb_continue($ftp_connection)==FTP_MOREDATA){
$contents = stream_get_contents($sockets[1]);
if($contents !== false) {
echo $contents;
flush();
}
}
if ($ret != FTP_FINISHED) $error[] = 'There was an error downloading the file...';
}else{
$error[] = "Could not ftp_nb_get file from $ftp_server";
}
}else{
$error[] = "Couldn't cd to $ftp_dir_new";
}
} else {
$error[] = "Couldn't connect as $ftp_user";
}
}else{
$error[] = "Couldn't connect to $ftp_server";
}
}else{
$error[] = "Unable to create socket pair";
}
if($sockets){
fclose($sockets[0]);
fclose($sockets[1]);
}
if( ! empty($ftp_connection)) ftp_close($ftp_connection);
}
So you're looking for an alternative to ftp_nb_get() that allows you to write to STDOUT or an arbitrary stream, instead of a local file?
I've not used this module at all, but it looks like ftp_nb_fget does what you want.
I know it's an old topic, but I came across this code and found small bug. Maybe someone uses it and has problem making it work.
This code won't work for very small files because ftp_nb_continue won't return FTP_MOREDATA even once. To fix this you need to change while loop to:
while($ret == FTP_MOREDATA){
$contents = stream_get_contents($sockets[1]);
if($contents !== false) {
echo $contents;
flush();
}
$ret = ftp_nb_continue($ftp_connection);
}
I have a script which connect my source FTP and destination FTP and copy all files from source to destination now i have change my server and it returns me these errors ftp connection suspenseful but didn't copy files just create files. My code is written below.
$ftp1 = new ClsFTP("$user1","$pass1", "$site1");
if(!$ftp1)
{
echo "could not connect";
exit;
}
$ftp2 = new ClsFTP("$user2","$pass2", "$site2");
$mod = "0755";
$dir_list[] = "httpdocs";
for($i=0;$i<count($dir_list); $i++)
{
echo "<br>Copying Directory: " . $dir_list[$i];
if(!$ftp1->cd($dir_list[$i]))
{
echo "1::$site1 could not open directory: $dir_list[$i]. skipping<br>\n";
continue;
}
if(!$ftp2->cd($dir_list[$i]))
{
echo "2::$site2 could not open directory: $dir_list[$i]. Trying to create it<br>\n";
if(!$ftp2->mkdir($dir_list[$i]))
{
echo "2::$site2 could not create directory: $dir_list[$i]. skipping<br>\n";
continue;
}
if(!$ftp2->cd($dir_list[$i]))
{
echo "2::$site2 could not open directory: $dir_list[$i]. skipping<br>\n";
continue;
}
}
if($dir_list[$i]=="httpdocs/tmpupload"){
}else{
$dir_rawlist = $ftp1->rawlist();
$dir_nlist = $ftp1->nlist();
$ftp1->p($dir_rawlist);
#$ftp1->p($dir_nlist);
for($k = 0; $k<count($dir_nlist); $k++)
{
if($dir_list[$i]=="httpdocs/includes" && $dir_nlist[$k]=="cert_key_pem.txt"){
continue;
}
if($dir_list[$i]=="httpdocs" && $dir_nlist[$k]=="PayPal.class.php"){
continue;
}
$size = $ftp1->check_file($dir_nlist[$k]);
if($size >0)//substr($dir_rawlist[$k], 0, 1)=='-')//its a file
{
$ftp1->get("tmp/".$dir_nlist[$k], $dir_nlist[$k]);
$handle = fopen("tmp/".$dir_nlist[$k], 'r');
$contents = fread($handle, filesize("tmp/".$dir_nlist[$k]));
fclose($handle);
$contents = str_replace($search_for, $replace_with, $contents);
$handle = fopen("tmp/".$dir_nlist[$k], 'w');
fwrite($handle, $contents);
fclose($handle);
$ftp2->put($dir_nlist[$k], "tmp/".$dir_nlist[$k]);
$ftp2->chmod($dir_nlist[$k], $mod);
}
}
}
$ftp1->cd("/");
$ftp2->cd("/");
}
These warnings thrown by script.
Warning: ftp_get(): Error opening tmp/InviteFriend2.php in /var/www/vhosts/virtualphoneline.com/httpdocs/includes/ftp.class.php on line 120
Warning: fopen(tmp/InviteFriend2.php): failed to open stream: Permission denied in /var/www/vhosts/virtualphoneline.com/httpdocs/includes/clone.php on line 228
Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/vhosts/virtualphoneline.com/httpdocs/includes/clone.php on line 229
Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/vhosts/virtualphoneline.com/httpdocs/includes/clone.php on line 230