PHP FTP Uploader Error.

They have: 45 posts

Joined: Feb 2002

Hey, I've got an error with this code, it returns saying that it cannot open the file, which is sent through this code from a form with a . Some of the code is kinda stretched out, when its working I'm planning on putting things more together.

Code:

<?php
$ftp
= ftp_connect (\"localhost\", \"21\");
 
$ftplog = ftp_login ($ftp, $ftpuname, $ftppass);

  ftp_chdir(
$ftp, \"www/scmaps/\");
 
$file_name = explode(\"\\\", $file);
 
$size = count($file_name);
 
$size2 = $size - 1;
 
$destination = \"motw/$file_name[$size2]\";
  ftp_put(
$ftp, $destination, $file, ftp_binary);
  ftp_quit(
$ftp);
  echo (\"<font face=\\"
verdana\\"><b>File Uploaded Successfully!</b></font>\");
?>

Error:
Warning: error opening C:\My Documents\untitled.bmp in /home/bboyera/eracreations-www/admin/add_map.php on line 23

Thanks a lot!

--Fm666

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

You didn't escape out one of the quotes in line 23.

 echo ("\"verdana\">File

They have: 45 posts

Joined: Feb 2002

Hmmm... well in my actual file the slash is there, the problem is it can't find the file on the remote computer or something.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

PHP Docs: Chapter 19. Handling file uploads

try something like this:

<?php
$ftp
= ftp_connect (\"localhost\", \"21\");
 
$ftplog = ftp_login ($ftp, $ftpuname, $ftppass);

  ftp_chdir(
$ftp, \"www/scmaps/\");
 
$destination = \"motw/\" . basename($HTTP_POST_FILES['file']['name']);
  ftp_put(
$ftp, $destination, $HTTP_POST_FILES['file']['tmp_name'], ftp_binary);
  ftp_quit(
$ftp);
  echo (\"<font face=\\\"verdana\\\"><b>File Uploaded Successfully!</b></font>\");
?>

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 45 posts

Joined: Feb 2002

Well, still isn't working. But the error changed.

<?php
$ftp
= ftp_connect (\"localhost\", \"21\");
 
$ftplog = ftp_login ($ftp, $ftpuname, $ftppass);

  ftp_chdir(
$ftp, \"www/scmaps/\");
 
$destination = \"motw/\" . basename($HTTP_POST_FILES['file']['name']);
  ftp_put(
$ftp, $destination, $HTTP_POST_FILES['file']['tmp_name'], ftp_binary);
  ftp_quit(
$ftp);
  echo (\"<font face=\\"
verdana\\"><b>File Uploaded Successfully!</b></font>\");
?>

Gave me this:

Quote: Warning: error opening in /home/bboyera/eracreations-www/admin/add_map.php on line 21

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

what is that file?

They have: 45 posts

Joined: Feb 2002

Well its in a file called, add_map.php and the file being uploaded can be anything.

Here is the entire file.

<?php
<html>
<
head>
  <
title>Want to add a new map?</title>
</
head>
<
body bgcolor=\"#000000\" link=\"#00AA00\" alink=\"#00AA00\" vlink=\"#00AA00\" text=\"#FFFFFF\">



$ftp = ftp_connect (\"localhost\", \"21\");
$ftplog = ftp_login ($ftp, \"UNAME\", \"PWORD\");

if (isset(
$file)){
  if (
$file==\"none\"){
    echo \"You must specify a file to upload\";
  }
  else {
    if (
$type == \"motw\") {

  ftp_chdir(
$ftp, \"www/scmaps/\");
 
$destination = \"motw/\" . basename($HTTP_POST_FILES['$file']['name']);
  ftp_put(
$ftp, $destination, $HTTP_POST_FILES['$file']['tmp_name'], ftp_binary);
  ftp_quit(
$ftp);
  echo (\"<font face=\\"
verdana\\"><b>File Uploaded Successfully!</b></font>\");

     /* ftp_chdir(
$ftp, \"www/scmaps/\");
     
$file_name = explode(\"\\\", $file);
     
$size = count($file_name);
     
$size2 = $size - 1;
     
$destination = \"motw/$file_name[$size2]\";
      ftp_put(
$ftp, $destination, $file, ftp_binary);
      ftp_quit(
$ftp);
      echo (\"<font face=\\"
verdana\\"><b>File Uploaded Successfully!</b></font>\"); */
    }
  }
}


<font face=\"verdana\"><b>Instructions:</b>  Please locate the map on your computer. 
Then select the type of file it is (this will put it in different directories). 
When done, pess upload.  And Whallah!<br>
<form action=\"add_map.php\" method=\"post\">
  <input type=\"reset\" value=\"Clear Form!\"><br>
  <input type=\"file\" name=\"file\"><br>
  <input type=\"radio\" name=\"type\" value=\"motw\">Map of the week<br>
  <input type=\"radio\" name=\"type\" value=\"melee\">Melee<br>
    &nbsp;&nbsp;&nbsp;<u>If Melee:</u><br>
    &nbsp;&nbsp;&nbsp;<input type=\"radio\" name=\"meleetype\" value=\"64x64\">64x64<br>
    &nbsp;&nbsp;&nbsp;<input type=\"radio\" name=\"meleetype\" value=\"128x64\">128x64<br>
    &nbsp;&nbsp;&nbsp;<input type=\"radio\" name=\"meleetype\" value=\"192x64\">192x64<br>
    &nbsp;&nbsp;&nbsp;<input type=\"radio\" name=\"meleetype\" value=\"256x64\">256x64<br>
    &nbsp;&nbsp;&nbsp;<input type=\"radio\" name=\"meleetype\" value=\"128x128\">128x128<br>
    &nbsp;&nbsp;&nbsp;<input type=\"radio\" name=\"meleetype\" value=\"192x128\">192x128<br>
    &nbsp;&nbsp;&nbsp;<input type=\"radio\" name=\"meleetype\" value=\"256x128\">256x128<br>
    &nbsp;&nbsp;&nbsp;<input type=\"radio\" name=\"meleetype\" value=\"192x192\">192x192<br>
    &nbsp;&nbsp;&nbsp;<input type=\"radio\" name=\"meleetype\" value=\"256x192\">256x192<br>
    &nbsp;&nbsp;&nbsp;<input type=\"radio\" name=\"meleetype\" value=\"256x256\">256x256<br>
  <input type=\"radio\" name=\"type\" value=\"ums\">UMS<br>
    &nbsp;&nbsp;&nbsp;<u>If UMS:</u><br>
    &nbsp;&nbsp;&nbsp;<input type=\"radio\" name=\"umstype\" value=\"rpg\">RPG<br>
    &nbsp;&nbsp;&nbsp;<input type=\"radio\" name=\"umstype\" value=\"mad\">Madness<br>
    &nbsp;&nbsp;&nbsp;<input type=\"radio\" name=\"umstype\" value=\"other\">Other<br>
  <input type=\"radio\" name=\"type\" value=\"campaign\">Campaign<br>
    &nbsp;&nbsp;&nbsp;<u>If Campaign:</u><br>
    &nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"campaign\">
<-- Enter campaign name (check spelling carefully!  If spelt wrong, the map will be put in the wrong directory!)<br>
    &nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"part\">
<-- Enter part number of the campaign.<br>
    &nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"partname\">
<-- Enter name of the part of the campaign.<br>
  <textarea name=\"description\" rows=\"10\" cols=\"40\">Add description here.  No matter what selected above.</textarea><br>
  <input type=\"submit\" value=\"Upload!\" name=\"upload\">
</form>
</font>
</body>
</html>
?>

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

You should move your ftp_connect() and ftp_login() inside the IF statement with the other ftp functions.

try this....

<?php
$ftp
= ftp_connect (\"localhost\", \"21\");
$ftplog = ftp_login ($ftp, $ftpuname, $ftppass);
ftp_chdir(
$ftp, \"www/scmaps/\");
$destination = \"motw/\" . basename($HTTP_POST_FILES['file']['name']);
$source = $HTTP_POST_FILES['file']['tmp_name'];
if (file_exists(
$source)) {
    ftp_put(
$ftp, $destination, $source, ftp_binary);
    echo (\"<font face=\\"
verdana\\"><b>File Uploaded Successfully!</b></font>\");
}
else {
    echo (\"<font face=\\"
verdana\\"><b>Error uploading file: $source</b></font>\");}
ftp_quit(
$ftp);
?>
PHP Docs: file_exists()

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 45 posts

Joined: Feb 2002

Okay, when I just tried that it printed the error uploading file part.

I changed the coed a tincy bit, in the if (file_exists($source)) { blah } part I added the not sign (!) in front of file_exists, like so if (!file_exists($source)) { blah }. Because before is the file already existed it would upload it and if it didn't then it wouldn't upload it. Am I correct there?

So it looks like this.

<?php
$ftp
= ftp_connect (\"localhost\", \"21\");
$ftplog = ftp_login ($ftp, \"bboyera\", \"hunter\");
ftp_chdir(
$ftp, \"www/scmaps/\");
$destination = \"motw/\" . basename($HTTP_POST_FILES['file']['name']);
$source = $HTTP_POST_FILES['file']['tmp_name'];
if (!file_exists(
$source)) {
    ftp_put(
$ftp, $destination, $source, ftp_binary);
    echo (\"<font face=\\"
verdana\\"><b>File Uploaded Successfully!</b></font><br>\");
}
else {
    echo (\"<font face=\\"
verdana\\"><b>Error uploading file: $source</b></font><br>\");}
ftp_quit(
$ftp);
?>

And when I run it it still return the error,
"Warning: error opening in /home/bboyera/eracreations-www/admin/add_map.php" on the line of ftp_put(). Is it possible that theres just something about my connection thats not letting it access the file I want to upload or something?

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

You can only upload a file that exists. What was the path that it gave without the bang (!)?

They have: 45 posts

Joined: Feb 2002

Oh, okay, I misinterpreted that.

The file is on my hard drive. C:\blah\blah.jpg Or something.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I need the exact path & filename...
What you should see, is that the path is your PHP upload directory, and the filename matches that of the original file. If it is something else, then there is something wrong with the upload.

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 45 posts

Joined: Feb 2002

Okay, I'm fairly new to all this and I'm not too sure what you're asking for so I'll just give you everything I can think of!

I'm trying to upload files from my hard-drive (or to be more specific I'm making this so other people can upload files from their hard-drives to the server). So the file would be starting in a place such as C:\maps\map1.scx on the remote server, then going into a directory such as \maps\motw\ on the local server (the one the file will be hosted on.

When I try to run the file, it doesn't work and returns an error in the ftp_put function when it tries to open the file.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

use this again:

<?php
$ftp
= ftp_connect (\"localhost\", \"21\");
$ftplog = ftp_login ($ftp, $ftpuname, $ftppass);
ftp_chdir(
$ftp, \"www/scmaps/\");
$destination = \"motw/\" . basename($HTTP_POST_FILES['file']['name']);
$source = $HTTP_POST_FILES['file']['tmp_name'];
if (file_exists(
$source)) {
    ftp_put(
$ftp, $destination, $source, ftp_binary);
    echo (\"<font face=\\"
verdana\\"><b>File Uploaded Successfully!</b></font>\");
}
else {
    echo (\"<font face=\\"
verdana\\"><b>Error uploading file: $source</b></font>\");}
ftp_quit(
$ftp);
?>
Then copy & paste the error you get. It should be "Error uploading file: " and then a path & filename.

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 157 posts

Joined: Mar 2002

No, no, no. Hold on. FTP!? To upload? Never needed that myself. Here's a direct quote from a file currently in use for a client of mine to uplaod pictures from his desktop to his site on the server.

if(isset($pic)){
$wdir="/";
$itemimages="/path/to/site.com/images"; //changed for this forum
if(move_uploaded_file($userfile,$itemimages.$wdir.$userfile_name)) {
echo"Picture was added to your product ";
}else{
echo"Failed to upload picture. Please contact your website administrator.";
}
}//end of upload

$userfile is the var. from the form:
$pic var. just tells if picture is to be added to product.

Have you tried this? Am I missing something here? Let me know, like I said, I haven't had to use the ftp_ thing.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

What I think he wants, is to upload a file to server1, then ftp that file from server1 to server2.

They have: 45 posts

Joined: Feb 2002

Okay, I guess I lost you guys. And I guess I was doing something really wrong.

I want to upload a file from my computer, to a website.

Like from:

C:\maps\map1.scx -> http://www.eracreations.com/maps/map1.scx

I thought you could do that with ftp_ things. I guess not.

Could you, 'theprofessional', type up some code thats equivalent to the code that I made? Like the same variable names and everything because I'm not to sure what to make of your code, as I said, I'm fairly new to all this.

They have: 157 posts

Joined: Mar 2002

I'll do my best. Be forwarned though, there are two ways to upload this way to your server according to your servers PHP version and .ini file setup. The 'move_uploaded_file(...' may also be 'copy(...'

if(isset($file)){ //or: if(is_uploaded_file){
if ($file=="none"){
echo "You must specify a file to upload";
}else{
if ($type == "mowt") $destination="/path/to/site.com/imagefolder"; //change this
if ($type == "melee") $destination="/path/to/site.com/otherimagefolder"; //change this
$wdir="/";
if(move_uploaded_file($file,$destination.$wdir.$file_name)) { //type '$file_name' exactly.
echo"File Uploaded Successfully!";
}else{
echo"Failed to upload picture. Contact the webmaster if this error persist.";
}
}
}//end of upload

Also, use this way to start the form, and include a file max size restriction.

And then use this tag format for the file.

Of course, this is simplified. Meaning there's a lot more to do to the entire code, i.e. From validation and file extensions security check. Anyways, hope this helps.

They have: 45 posts

Joined: Feb 2002

Well, it seems to be working it just says it doesn't have permission to create the file in the directory, even though the directory is chmoded so that anyone can.

In the $destination variables, what should the path look like if I want to upload to /home/username/www/scmaps/motw (or melee) and the .php file (where the code is for the upload is /home/username/www/admin/add_map.php

taff's picture

They have: 956 posts

Joined: Jun 2001

Is this your server you are dealing with or a host? There have been some recent security vulnerabilities involving the PHP file upload feature and a number of hosts have disabled the feature because of it.

I think I have a relevant link here somewhere...

Ah, voila!

http://www.cert.org/advisories/CA-2002-05.html

.....

They have: 45 posts

Joined: Feb 2002

A host. I'll contact them and see if they have disabled the features or if they applyed the patch.

They have: 157 posts

Joined: Mar 2002

taff, your correct. My host has this turned off for me too but, this is the code to use in those circumstances. And I know it works because I have two clients that use file uploads on the same host.

/home/username/www/scmaps/motw doesn't look right. I'm assuming that scmaps is your domain? Ask your host for the correct file path on the server to your site. And if your trying this with your own server setup with Apache or something, forget it. Test the code online, live on your hosted server. In addition, don't try to upload from one domain to another, stay within the same domain. Plus, if your domain name is a pointer there may be a server error that the admin. will have to fix. Try it again with these things in mind and if it doesn't work again, you'll need to get the host admin to help because it might be on his end.

Post your fix if you become successful. Back when I was first trying to get file uploads to work for me, it took an entire week of research and hair pulling, but I did it on my own. There are a lot of if's and variables to consider when coding for this, it is very frustrating.

They have: 45 posts

Joined: Feb 2002

the /home/username/www/... is from the FTP managers. Such as WS_FTP95 LE.

It starts out in the home/username/ directory, and from there there is a couple directorys, one of which is for mail, another is for logs etc. And one is www which is where regular files are. The domain name is eracreations.com and I'm trying to upload maps into eracreations.com/scmaps/motw (or melee, depending on whats selected.

They have: 157 posts

Joined: Mar 2002

/home/username/www/scmaps/motw still doesn't look right according to your domain name. It looks as though the host keeps thier hosted in the username dir. and your the only one with www as your dir. name, which is different than eracreations. Ask your host specificly for the path to your site on his server.
Also, I can't believe you can see the server in your FTP up from your own host dir. I wonder if they know about this. Sounds like a security leak to me. Go to /home and see all the other hosted sites dir.'s. Bet you can steal some files. I had a host like that once. I could FTP to every site on his server and look at everones files. Pretty cool, but dishonost.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I think that 'username' means the person's username, and not literally 'username'. So,assuming his username is 'ecreations', his path would be /home/ecreations/www/scmaps/motw/

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 45 posts

Joined: Feb 2002

Yes, Mark's correct.

I cannot FTP up past the www directory.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.