I'm having trouble uploading an image from an HTML form to SFTP server. I'm using phpseclib to achieve this.
<?php
include('Net/SFTP.php');
$uploaded_file = $_FILES["my_image"]["tmp_name"];
$sftp = new Net_SFTP('my_server', 'my_port');
if (!$sftp->login('my_username', 'my_pass')) {
exit('Login Failed');
}
$sftp->mkdir('/home/new_dir');
$sftp->put($uploaded_file,'/home/new_dir/'.$uploaded_file, ), NET_SFTP_LOCAL_FILE);
?>
The connection is there and I can create a directory successfully, therefore I assume that the problem is here:
$sftp->put($uploaded_file,'/home/new_dir/'.$uploaded_file, ), NET_SFTP_LOCAL_FILE);
According to the documentation the order of your arguments should be the other way round.
$sftp->put('/home/new_dir/'.$uploaded_file, $uploaded_file, NET_SFTP_LOCAL_FILE);
Related
I could not copy the file with spaces in file name using ssh2_scp_recv() function.This is the filename testfile-03_23_15 11 02 AM.csv which actually stored in server.
my code is here
if ($file == "testfile-03_23_15 11 02 AM.csv"){
if(!ssh2_scp_recv($connection,$remoteDir .$file, $localDir . $file)){
echo "Could not download: ", $remoteDir, $file, "\n";
}
}
Please help me if you know.
Thanks.
With phpseclib:
<?php
include 'phpseclib/Net/SSH2.php';
include 'phpseclib/Net/SCP.php';
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('bad login');
}
$scp = new Net_SCP($ssh);
$scp->get('file name with spaces');
Try this one:
ssh2_scp_recv($connection,"\"".$remote_file_name."\"",$local_path."/".$remote_file_name);
Source: php.net
It says: Trying to get a remote file with spaces in its name?
Never forget to use quotes in the remote filename, but never in the local one.
I can successfully upload / download image files in Mongodb with php through GridFS, but when I try to upload mp3 file, nothing happens. Neither I get any error nor any it is uploaded and further I have checked the permission of the file it's very similar of those image files plus the file size is also not too big it is around 6mb only. But for unknown reason it is not being uploaded in the Mongodb database through GridFS with php. Please help me loading the MP3 files to the database. The code used for uploading files is as below :
<?php
error_reporting(0);
$m = new MongoClient();
$gridfs = $m->selectDB('mydb')->getGridFS();
$gridfs->storeUpload('pic', array('_id'=>$_POST['_id'],'Username' => _POST['username'] ));
echo 'File Uploaded Successfully';
?>
Instead of storeUpload use storeFile as given below example
ex:
<?php
// connect to the ‘myGrid’ GridFS
$m = new Mongo();
$db = $m->myDB;
$myGrid = $db->getGridFS('myGrid');
$some_file='path of your video or audio file';
// some extra data you may want to store with your file
$data_array = array(
'mime' => mime_content_type($some_file),
'timestamp' => time(),
);
// store a file into the GridFS
$myGrid->storeFile($some_file, $data_array);
?>
I am using the PECL ssh2 module to output XML data to a sftp server. I have two entirely separate PHP scripts which gather different data and send the output to different file on the stfp server.
CUSTOMER EXPORT:
$conn = ssh2_connect(SFTP_SERVER, SFTP_PORT);
ssh2_auth_password($conn, SFTP_USER, SFTP_PWD);
$sftp = ssh2_sftp($conn);
$file = 'ssh2.sftp://' . $sftp . CUSTOMER_EXPORT_PATH . CUSTOMER_EXPORT_FILENAME;
$doc = new DOMDocument('1.0','UTF-8');
CustomerExportXML($doc);
if (file_exists($file)) {
unlink($file);
}
$bytes_saved = $doc->save($file);
PRODUCT EXPORT:
$conn = ssh2_connect(SFTP_SERVER, SFTP_PORT);
ssh2_auth_password($conn, SFTP_USER, SFTP_PWD);
$sftp = ssh2_sftp($conn);
$file = 'ssh2.sftp://' . $sftp . PRODUCT_EXPORT_PATH . PRODUCT_EXPORT_FILENAME;
$doc = new DOMDocument('1.0','UTF-8');
ProductExportXML($doc);
if (file_exists($file)) {
unlink($file);
}
$bytes_saved = $doc->save($file);
In each case the XxxExportXML($doc) function takes a couple of minutes to gather the relevant data and stuff it in to $doc.
Each script works as is and exports the correct data to the correct place.
The problem is when their execution overlaps only the last one executed actually writes to the sftp server. If I echo out the $file variable then in each case they both have the same resource ID ie ssh2.sftp://ResourceID#150/Customer/Customer.xml and ssh2.sftp://ResourceID#150/Product/Product.xml
So my question is why are these two processes interfering with each other and how do I fix it so they can both be run at the same time?
So they're two different scripts? That's... weird. Maybe try phpseclib, a pure PHP SFTP implementation, instead. eg.
<?php
include('Net/SFTP.php');
$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
exit('Login Failed');
}
// puts a three-byte file named filename.remote on the SFTP server
$sftp->put('filename.remote', 'xxx');
I'm using phpseclib - SFTP class and am trying to upload a file like so -
$sftp = new Net_SFTP('mydomain.com');
if (!$sftp->login('user', 'password')) {
exit('Login Failed');
}
$sftp->put('/some-dir/',$fileTempName);
The file however isn't being uploaded inside some-dir but is uploaded one directory before (to the starting directory, lets say it's root). This is driving me crazy, I think I've tried all combinations of some-dir/ or /some-dir or /some-dir/, but the file won't upload there.
I don't think your put is doing what you think it is doing. According to the docs, you need to do a Net_SFTP::chdir('/some-dir/') to switch to the directory you want to send file to, then put($remote_file, $data), where remote_file is the name of file, and $data is the actual file data.
Example Code:
<?php
include('Net/SFTP.php');
$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
exit('Login Failed');
}
echo $sftp->pwd() . "\r\n";
$sftp->put('filename.ext', 'hello, world!');
print_r($sftp->nlist());
?>
I am new to PHP and here is my dilemma. I am trying to add to my site a feature that allows users to select a set of images in Flash (AS3) click a button and have my server create a zip file of all the images for them to download.
I have already wrote my code in flash and crated my php script and tested it on my localhost and everything works great. But when I upload the same files to my sever no zip files are created. Below is the code for both my flash and for my php. Any help or tips would be appreciated I am struggling over her.
Localhost server is using 5.2.17 and so if my Server hosted on 1&1
Flash Code To Send Info to PHP
function MakeZip():void
{
var variables:URLVariables = new URLVariables();
//variables.Image1=img1;
variables.ImageList=OrderItems;
variables.Name= zipName_txt.text + ".zip";
variables.FileComplete="File Created";
var request:URLRequest = new URLRequest();
request.url="ArrayTest.php";
request.data=variables;
var loader:URLLoader = new URLLoader();
loader.load(request);//sends the request
//when the request is done loading, it goes to the completeWriting function
loader.addEventListener(Event.COMPLETE, completeWriting);
loader.addEventListener(IOErrorEvent.IO_ERROR, error);
function completeWriting(event:Event):void
{
info_txt.text = "File Created";
}
function error(e:IOErrorEvent):void
{
info_txt.text = "There was an error. Please try again later.";
}
}
PHP to create file
<?PHP
$zipName= $_GET['Name'];
$Order= $_GET['ImageList'];
$fileList = explode('|',$Order);
$ZipComplete = $_GET['FileComplete'];
// create object
$zip = new ZipArchive();
// open archive
if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
// add files
foreach ($fileList as $f) {
$zip->addFile($f) or die ("ERROR: Could not add file: $f");
print false;
}
// close and save archive
$zip->close();
//writeVariable( "ZipComplete", $ZipComplete);
echo "FileComple=" . $ZipComplete;
?>
Thank you all for your tips and hints. After hours of trying different things I found the mistake was in my flash code. On my localhost it ignores capitalization for file names. On my server however it does not ignore it. I had in my flash file image1.jpg but the file name is actually Image1.jpg. Glad I found it bit it is so frustrating how a little thing like that made it not work.
is there any error response returned from you php script?
please make sure the zip file save path on server is correct and have write access
This is because code is running as "nobody" on server. This user would not have write access to anywhere except /tmp .
Add following code, this should work:
<?PHP
$zipName= $_GET['Name'];
$Order= $_GET['ImageList'];
$fileList = explode('|',$Order);
$ZipComplete = $_GET['FileComplete'];
// create object
$zip = new ZipArchive();
$zipName = "/tmp/" + $zipName;
// open archive
if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
$zipName = "/tmp/" + $zipName; is the key part.