View Full Version : upload file script = website A... file destination = website B... how?

dotted line
01-17-2004, 05:09 PM
Because of server complications I have set up a Control Panel for a customer's website on my own website. I have their login information so if that is all I need to get this to work properly, that is not a problem.

What I need to do is have the uploaded file (handled and processed through my website) sent to a directory on the customer's server. Right now, it is not allowing me to do that, obviously I have coded something wrong where it tries to upload it...

Here is the part of my script where I believe the error is occuring:

if (isset($_POST['submit'])) {

$sql = "INSERT INTO salesTalk (fileName, fileSize, fileType, uploadDate) VALUES ('{$_FILES['upload']['name']}', {$_FILES['upload']['size']}, '{$_FILES['upload']['type']}', NOW())";
$result = @mysql_query ($sql);

if ($result) {
$extension = explode ('.', $_FILES['upload']['name']);
$iid = mysql_insert_id();
$filename = $iid . '.' . $extension[1];

if (move_uploaded_file($_FILES['upload']['tmp_name'], "http://www.my.customer.org/salesTalk/$filename")) {
echo '<span class="textNorm">Thank you, the file has been uploaded. </span>';
} else {
echo '<span class="error">The file could not be stored. </span>';

$sql = "DELETE FROM salesTalk WHERE issueId = $iid";
$result = @mysql_query ($query);
} else {
echo '<span class="error">We\'re sorry, a system error occurred. Please try again. </span>';



echo '
<form enctype="multipart/form-data" action="'; ?><?php echo $_SERVER['PHP_SELF']; ?><?php echo '" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288">
<table cellpadding="0" cellspacing="0" align="center">
<td align="left" class="textNorm">File: </td>
<td align="left" class="textNorm"><input type="file" name="upload" size="16" maxlength="20" value="'; ?><?php if (isset($_POST['firstName'])) echo $_POST['firstName']; ?><?php echo '" /></td>
<td colspan="2" class="textNorm" align="center">
<input type="submit" name="submit" value="Upload" />

Thank you for any and all help on this matter!

edit: I think (one of) the problem(s) was that the directory permissions were not set correctly. The directory is now chmoded to 777 but I'm still getting the error.

01-17-2004, 07:59 PM
if (move_uploaded_file($_FILES['upload']['tmp_name'],
"http://www.my.customer.org/salesTalk/$filename")) {

This line should not work. AFAIK, you can't move files to URL destinations. You rather need a filesystem path, like


Try out both relative and full paths, and see if this measure makes your app work.

dotted line
01-17-2004, 08:08 PM
But the destination is on a completely different server, how would I specify the filepath to that?

01-17-2004, 08:16 PM
FULL path

dotted line
01-17-2004, 08:22 PM
Thank you for your help, both of you.

Would the full path begin with http:// ?
I've never done this before so sorry for the stupid questions.

01-17-2004, 08:28 PM
Sorry dotted line, I did not read your original post closely enough. Anyway, you can't "move" uploaded files to other servers. At least not with move_uploaded_file(). What you need is to re-upload the file to the destination server. That also requires a script to be on the destination server which handles the incoming file data.

So now you have the problem how to send the file to another server. You can use FTP functions from within PHP, if you happen to have a FTP account for the target server. Or you use a command line application by exec(), though that makes your script not very portable.
Additionally, you could open a socket (with fsockopen()) and send the file to the destination server by establishing a HTTP connection and sending it via POST. Another option would be to use the cURL extension.

All what I've said is essentially doable only if the necessary requirements are met. That is to have either FTP, sockets or cURL installed at your "a" server, and the ability to have a script running at "b" server. If you tell us what of these options your server architecture supports, we can give more detailed help.

dotted line
01-17-2004, 08:38 PM
I just noticed that my php.ini file has the temp upload dir commeneted out:
;upload_tmp_dir =

So actually, that appears to be the underlying problem. Any idea what I should do about this, or should I just email the hosting company?

After that is worked out, FTPing the files would work fine!

01-17-2004, 08:45 PM
The directory for storing the uploads temporarily must be set, and you must have read/write access to it. Check your open_basedir setting as well. Either you do these adjustments yourself and restart the server, or you have to ask your host to do this. They should, if they sold you a hosting packages that includes standard PHP functionality. :)

There are a couple of other settings that affect file uploads too. Be sure to check the configuration notes mentioned on http://us2.php.net/manual/en/features.file-upload.php and probably include them in your request to the host as well.

01-17-2004, 08:56 PM
Heh, talk about quick replies. I was just about to say what mordred said about using ftp and such, good job I pressed the preview message button instead of reply :)

It's a good job you can't upload files in the way you tried dotted line, it would create chaos :) You'd be able to upload all sorts of files to other sites, like replacing yahoo's index page with your own index page heh

dotted line
01-18-2004, 08:01 AM
Heh, yeah Nightfire, that would be bad. I pretty much assumed that was part of the problem from the start, I just didn't know how to correct it.

Mordred, or anyone else who wants to help:

I have gotten uploads to work. Now I need to ftp them to another server. I have spent the past hour or so trying to do this but it's done nothing but give errors and make me want to go to bed.

Thanks for the help, I'll check back in the morning.

01-18-2004, 10:21 AM
I myself have not much experience with FTP functions in PHP (seldom needed them), but this modified script from the manual works alright for me:

$ftp_server = 'server.example.com';
$ftp_user_name = 'username';
$ftp_user_pass = '******';
$source_file = 'file.txt'; // filesystem path on first server
$destination_file = 'file.txt'; // filesystem path on second server

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!<br>";
echo "Attempted to connect to $ftp_server for user $ftp_user_name<br>";
} else {
echo "Connected to $ftp_server, for user $ftp_user_name<br>";

// upload the file (either in ASCII or Binary mode)
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_ASCII);

// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";

// close the FTP stream

dotted line
01-19-2004, 03:56 AM
Ahh. Ok, let's nix the ftp connection.

What am I doing wrong here...

fopen("ftp://user:pass@host/path/" . $filename, "w");

This creates a blank file with the correct name. What mode do I have to use so that it sends the actual file? I have a feeling I'm not doing this right at all.

01-19-2004, 07:26 AM
Although you've got ftp in the url, you're not actually doing as me and mordred said :) You need to use the ftp functions php has. Use mordreds's script, fill in with your details and merge it in with your script. More info on ftp functions (http://uk.php.net/ftp)

dotted line
01-19-2004, 04:43 PM
I can't use the ftp commands in php with my host. Sorry for not being clear on that. What about fopen() or something else?

01-19-2004, 04:46 PM
Can't use fopen or anything as you'd not have the right permissions. Do you have sockets available? If not then there's no other way to do it apart from manually ftp'ing

dotted line
01-19-2004, 05:00 PM
Hmm, well, fopen works except that the transferred file has a size of 0kb. So I guess that I have the right permissions if I can do that, right?

01-19-2004, 05:46 PM
You can also use fopen wrappers to write to a FTP stream. But this is not really comparable to the rich interface of the FTP functions. Basically, you open a file by the FTP protocol and write bytes into it. To answer your question, there is no special mode to send files - that is provided by FTP functions. You need to read the content of file A, open file B through FTP, and write the content of A into B.

$fp = fopen("ftp://user:pass@server/" . $destination, "w");
fwrite($fp, file_get_contents('file.txt'));

if file_get_contents() is not supported on your server, try

fwrite($fp, implode('', file('file.txt')));

instead. Be aware that you can't overwrite existing files this way prior to PHP5, and you can't append to a file.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum