Question:
Script For Uploading Files Through URL?
anonymous
2012-06-10 22:13:17 UTC
I need a php script which will enable me to upload files to my server through an URL.
I mean,If i put an URL of any file (e.g. http://mysite.com/pic.jpg) it will then upload the file to my server.If you know the script,then please help.
Three answers:
?
2012-06-10 22:36:34 UTC
PHP isn't really the *best* way to go about this, but it will work. The most likely solution to work would be:



$file = file_get_contents('http://www.stuff.com/pic.jpg');



This will get the contents and store it as a variable. From here, you can use file output commands to save this as a file on the server the PHP script runs on, like so:



$filename = 'savePic.jpg';

$fileHandle = fopen($filename, 'w');

fwrite($fileHandle, $file);

fclose(fileHandle);



Now what this does is saves the contents of $file to the file savePic.jpg on the server, in the same location the script was run. First we defined the filename, then we created a handle to a file we created and opened with the fopen command. Here, w stands for write. Likewise, a would be append and r would be read. After this, we wrote data to the file handle we created, then closed the file. Be sure to close any file you open, or wonky things will happen, most of them bad.
anonymous
2016-10-21 07:00:49 UTC
as far as i understand, this is not available. you need to upload records from the FTP application to dissimilar which you will bypass to in information superhighway Explorer...and vice versa. i could prefer to be incorrect besides the reality that, yet i think of of the source you study that from could have made a mistake.
Jim
2012-06-10 22:32:05 UTC
this will upload 1 file, but not through a URL. what you are asking for can't be done without an account.



function upload_put($conn_id, $dest_file, $src_file) {

global $remote_public_html_dir, $current_dir, $ftp_errors;

echo "upload_put(conn_id=$conn_id, dest=$dest_file, src=$src_file)\n";

$tries=50;

$err=false;

//echo "$conn_id dest=".basename($dest_file)." src=$src_file\n";

echo "$conn_id src=$src_file\n";

if (ftp_put($conn_id, basename($dest_file), $src_file, FTP_BINARY)) {

//echo "success: dest=".basename($dest_file)." src=$src_file\n";

echo "success: src=$src_file\n";

//echo "Uploaded $src_file to $host as $dest_file\n";

$err=false;

} else {

echo "--------!FTP upload has failed!----------\n";

$err=true;

$tries--;

$ftp_errors++;

}

while ($err == true && $tries > 0) {

echo "retry: ";

if (ftp_put($conn_id, basename($dest_file), $src_file, FTP_BINARY)) {

//echo "success: dest=".basename($dest_file)." src=$src_file\n";

echo "success: src=$src_file\n";

//echo "Uploaded $src_file to $host as $dest_file\n";

$err=false;

} else {

//echo "--------!FTP upload has failed!----------\n$conn_id dest=".basename($dest_file)." src=$src_file\n";

echo "--------!FTP upload has failed!----------\n($conn_id src=$src_file)\n";

$err=true;

$ftp_errors++;

$tries--;

}

}

}



//ftp upload file

function uploadFile($conn_id, $dest_file, $src_file) {

global $remote_public_html_dir, $current_dir, $ftp_errors;

//echo "uploadFile(dest=$dest_file, src=$src_file)\n";

if ($current_dir != $remote_public_html_dir . dirname($src_file)) {

// change directory first

if (@ftp_chdir($conn_id, $remote_public_html_dir . dirname($src_file))) {

$current_dir=$remote_public_html_dir . dirname($src_file); //only do this if chdir was successful.

echo "chdir to " . $remote_public_html_dir . dirname($src_file).": " . ftp_pwd($conn_id) . "\n";



//since change directory was successful, upload file

//and pre-allocate space if we can.

if (ftp_alloc($conn_id, filesize($src_file), $result)) {

upload_put($conn_id, $dest_file, $src_file);

} else {

echo "--------!Couldn't allocate space!--------\n";

upload_put($conn_id, $dest_file, $src_file);

}



} else {

echo "---------!Couldn't change directory!----------\n";

echo "doing mkdir ".dirname($src_file).":\n";

if (dirname($src_file)!="" && FALSE===ftp_mkdir($conn_id, $remote_public_html_dir . dirname($src_file))) {

$ftp_errors++;

return;

}

if (ftp_chdir($conn_id, $remote_public_html_dir . dirname($src_file))) {

$current_dir=$remote_public_html_dir . dirname($src_file); //only do this if chdir was successful.

echo "chdir to " . $remote_public_html_dir . dirname($src_file).": " . ftp_pwd($conn_id) . "\n";



//since change directory was successful, upload file

//and pre-allocate space if we can.

if (ftp_alloc($conn_id, filesize($src_file), $result)) {

upload_put($conn_id, $dest_file, $src_file);

} else {

echo "--------!Couldn't allocate space!--------\n";

upload_put($conn_id, $dest_file, $src_file);

}

} else {

echo "-----------!still couldn't change directory after mkdir!----------\n";

$ftp_errors++;

}

}

} else { //directory hasn't changed. don't chdir.

//since change directory was not needed, upload file

upload_put($conn_id, $dest_file, $src_file);

}

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...