How to allow cross-domain file uploads? - php

I have an html5 uploader thanks to the following tutorial:
http://www.profilepicture.co.uk/ajax-file-upload-xmlhttprequest-level-2/
works great.. however I would like to upload the files to a different domain... I thought this would be possible as long as the domain, or more specifically the file on the domain I was uploading too had the follwong header:
header("Access-Control-Allow-Origin: *")
Therefore allowing cross domain sharing...
However the upload is not working, is there anything else I am missing, or is it a case that you can communicate across domains but you can't upload files?
Kind regards to any responders...
J

I believe the best option for cross domain upload is to use ftp upload (of course you need to know ftp access credential such as ftp host, username and password.
If your using php as server side language you can try this little piece of code.
I use this on multiple domains within the same web server.
$conn_id = ftp_connect($server) or die("<span style='color:#FF0000'>Can't connect to ".$server."</span>");
$login_result = ftp_login($conn_id, $username, $password) or die();
$upload = ftp_put($conn_id, $server_path, $file, FTP_BINARY);
if (!$upload) {
echo "Error sending image to ".$server;
}
Hope this can help you.

Related

Transfer files between two remote FTP servers in PHP

First of all, I know that this a duplicate topic, but the other post that I found were not useful for my situation, so I decided to create a new one.
What I'm trying to accomplish is to get a file from one FTP server and upload it to another FTP server.
I'm using this code:
$ftp_server = "ftp_server";
$ftp_user_name = 'ftp_username' ;
$ftp_user_pass = 'ftp_pass' ;
$localDir = "full/path/";
$serverDir = "full/path/";
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_get($conn_id, $localDir, $serverDir, FTP_BINARY)) {
// ftp_fput($conn_id, $file, $fp, FTP_ASCII))
}
The problem that I have is when you use ftp_put command, it requires a local file, but this file it's not on my computer, so I can't upload it to the other ftp.
Is there a way to upload the file that I just got with ftp_get function into another server using ftp_put? Without the need to download it first on your PC?
Thanks!
Both ftp_get and ftp_put can operate with files only, not folders.
Use ftp_get to download a file from the first server to a local temporary folder/file. And then use ftp_put to upload the temporary file to the second server.
If you want to avoid using a temporary file, you can download the file to memory using ftp_fget and re-upload to the second server using ftp_fput.
PHP: How do I read a .txt file from FTP server into a variable?
Transfer in-memory data to FTP server without using intermediate file

How to use FTP to connect to Azure scale set Instance

So i have created a scale set in Azure (2 windows server 2016 VMs). I want to have a PHP application running on them. I want to know if it is possible to use an FTP connection to remotely upload/edit my php files which are going to be on the VMs. If not, what are the others ways i can use to remotely edit/upload my php files?. This is my first time working with a scaleset. thanks
Yes it's possible to use FTP in php:
//open a connection
$conn = ftp_connect($host);
//login to server
ftp_login($conn, $user, $pass);
//upload file
if (ftp_put($conn, $remoteFile, $localFile, FTP_ASCII)) {
return "<p>successfully uploaded $localFile to $remoteFile</p>";
} else {
return "<p>There was a problem while uploading $remoteFile</p>";
}
See the manual for more FTP functions http://php.net/manual/en/book.ftp.php

php big upload via ftp

i have to trasfer file via ftp with php. The files are big (also over 500MB).
So i think use php with ftp.
<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "localfile.txt";
// upload file
if (ftp_put($ftp_conn, "serverfile.txt", $file, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
?>
I have to know if the timeout is considered also in ftp trasfer.
If i trasfer the file via php page like upload.php with the code write, i have the execution time limit setting on the web server ?
You must take too many considerations before doing this operation, as mentioned below the first thing you need to check if your FTP server allows 500MB for uploads, the second thing you must set some PHP directives like post_max_size and max_upload_size, I suggest to use an FTP library to make it easier to do that, this a great one php-ftp-client that I've build.
Enable the feature on server side, which allows resuming file uploads.
From php you might be able to call the ftp with command_line options using functions like system(), or exec().
And, yes, in php there are limits: max_upload_size, max_execution_time, max_post_size and etc.
You will have to split the whole process into steps, with reloading of the page - if needed.

PHP Getting Media Information From A Remote File

So here's the setup. I'm storing and streaming videos on my website. I have a web server where most of the interaction is done and a media server which stores and streams the video files.
I'm trying to write a script on the web server that will list out all of the videos on the media server and get all the information about each video.
I can use the ftp commands through PHP to get the list of files on the media server without any problem. What I am looking to do is also get media information from each remote file such as video duration, codec, bitrate, etc. Not sure if this is possible, but if anyone knew how, it would be someone here on StackOverflow.
Here is the code that I currently have:
$conn = ftp_connect($media_server) or error_out('Failed to connect to FTP server');
ftp_login($conn, $username, $pwd) or error_out('Failed to login to FTP server');
$files = ftp_nlist($conn, ".") or error_out('Could not retrieve list of files');
foreach($files as $file){
$return[] = array('file_name' => $file,
'file_size' => ftp_size($conn, $file));;
}
ftp_close($conn);
echo json_encode($return);
exit();
function error_out($text){
echo '{ "error":"'.$text.'" }';
exit();
}
UPDATE:
I tried the class getID3 at http://getid3.sourceforge.net/ but unfortunately it does not support remote files. The files have to be on the same server.

FTP to a subfolder

Is it possible to upload a file into a subfolder on an FTP server?
I send a file daily to an FTP server, scheduled on a cron job that runs a php file. It all works fine, but now I've been asked to change the destination to a subfolder.
Changing *$server_name = 'ftp.website.com'* to *$server_name = 'ftp.website.com/data'*
doesn't seem to work?
I can see the folder exists in filezilla and it has all read/write access in permissions.
I'd be grateful if someone can advise if its a permission issue or whether it's possible at all?
Thanks in advance.
CODE...
//FTP bit...
$server_name = 'ftp.website.com';
$server_username = 'xxxx';
$server_password = 'xxxx';
$conn_id = ftp_connect($server_name);
$login_result = ftp_login($conn_id, $server_username, $server_password);
ftp_pasv($conn_id, true);
ftp_put($conn_id, $zip_filename, $output_dir . $zip_filename, FTP_BINARY);
Seems like you need to use ftp_chdir().
Connect as normal, and then change directories using ftp_chdir().
I assume you are using the ftp functions of PHP. I see two options:
1) Use ftp_put and specify the absolute path on the ftp server. http://php.net/manual/de/function.ftp-put.php
2) Before uploading your file change the working directory on the ftp server using ftp_chdir http://php.net/manual/de/function.ftp-chdir.php
If you use ftp_put you simply specify the absolute destination path on the server
http://php.net/manual/en/function.ftp-put.php

Categories