Link to home
Start Free TrialLog in
Avatar of evco
evcoFlag for United States of America

asked on

Uploading Files and mkdir()

I am working on an admin section for a site that resells vitamins. I have an "Add Product" page which allows the user to enter product information and upload an image for that new product.

I use the php mkdir() function to create a new folder if neccessary and then upload the image into that new folder using move_uploaded_file(). When I call mkdir() from the script I set the permissions of the folder to 750. When I then access the new folder (that was created with the script) using either WS_FTP or the Web Interface File Manager that the hosting company provides I find the folder filled with other files and folders that have not been put there by me or my script. Some of the folders are: cgi-sys, db, backend, frontend, and many many more.. I have called my hosting provider and they told me that it is my script. I also cannot delete the new folder that is created by my script.

I have read that when using move_uploaded_files, PHP checks whether the files or directories you are about to operate on have the same UID (owner) as the script that is being executed. If I don't set the permissions on the new dir to at least 750 I cannot upload the file to the new dir. I created the script and uploaded it using WS_FTP and the new dir is being created by that script. I have no idea how to check the UID for either the script or the new dir.

I would really appreciate any information about UID's, permissions, mkdir(), move_uploaded_files() or anything that can help to solve the problem. I have spent so much time on something that seems so simple. Help!
Avatar of evco
evco
Flag of United States of America image

ASKER

Or maybe a better way to go about creating a new dir and uploading files...
Avatar of lozloz
lozloz

hi,

that sounds really strange to me, but you certainly could replace move_uploaded_file() for copy(). i don't see why these folders would be created by mkdir, chmod or move_uploaded_file; so i think your hosting company is wrong.. anyway try changing to copy() and see if anything changes.

loz
Avatar of evco

ASKER

Thanks loz. I was hoping the hosting company would shed a little light, but oh well.

Instead of copy() I just found that I could use ftp in the script to upload the file and I am almost there so I guess I answered the question myself but I would like to know what you think of this workaround. I feel uneasy about using ftp through the script, I am not sure why but that is just a feeling I get. loz, could you give me your opinion....if you have one on the subject. Thanks!
ASKER CERTIFIED SOLUTION
Avatar of lozloz
lozloz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of evco

ASKER

I am using the function below which doesn't take too long. But like I said I would much rather use the common functions. It's just that I have spent so much time on this problem and am quite frustrated at this point so I figured I would settle for a workaround. But I guess I should solve the problem in order to learn, especially in case I need to implement something similar in the future. I am just a little paranoid since using mkdir() made such a mess of things. I will toss the ftp thing and continue to work on trying to solve the problem taking out the basics and debugging, like you said loz. Since I am back to the original question at hand am I taking advantage if I leave the Q open until I solve the problem?

Thanks for your input loz!

function ftpmkdir($path, $newdir) {
  $ftp_server='ftp.inyourbesthealth.com'; // ftp server
  $connection = ftp_connect($ftp_server); // connection

  // login to ftp server
  $user = "username";
  $pass = "password";
  $result = ftp_login($connection, $user, $pass);

  // check if connection was made
  if ((!$connection) || (!$result)) {
    return false;
    exit();
  } else {
    ftp_chdir($connection, $path); // go to destination dir
    if(ftp_mkdir($connection,$newdir)) { // create directory
      return $newdir;
    } else {
      return false;      
    }
    ftp_close($connection); // close connection
  }
}
leave the question open until you've got a satisfactory answer

ah, i didn't know there were ftp functions in php since i've never needed to use them. i thought you were doing the copying of the file over ftp too, if you've narrowed down the fault to the mkdir function then i guess it should be a bit easier to work on. i still think it might be an automatic thing by the host when folders are created, maybe some other people will have more to offer

until tomorrow,

loz
Avatar of evco

ASKER

great thanks for all your help so far!!
Avatar of evco

ASKER

Sorry, I almost forgot about this post.

I did what you said loz and stripped the script down to just the basics and went from there. I don't know if the whole problem was just a fluke or simply a syntax error on my part but the code is working now.  For those who may ever have this odd problem I am sorry to say that I am not sure exactly what the problem was but here is the code which is now working properly:

$file_name = $_FILES['file']['name'];
$file_type = $_FILES['file']['type'];
$file_size = $_FILES['file']['size'];
$file_tmp = $_FILES['file']['tmp_name'];
$images_folder_path = "/path.../.../";
$dest_filepath = $images_folder_path.$company_id."/" . $file_name;
$src_filepath = $file_tmp;
            
if ($file_name) {
  $product_img = "images/$company_id/".$file_name;
  if ((($file_type == "image/gif") || ($file_type == "image/jpeg") || ($file_type == "image/pjpeg")) && ($file_size < 60000)) {
    if (file_exists($dest_filepath)) {
      $display_fileinfo = "$file_name <span id=\"message\"> already exists.</span> ";
    }else{
       if (!file_exists("/blah/")) {
         mkdir("/dir/dir/dir/images/$company_id", 0755);
       }
       move_uploaded_file($src_filepath, $dest_filepath);
    }
}else{
    $_SESSION['error'] .= "<p id=\"error\">Only .gif and .jpg images under 60kb allowed. <br />(Image Type: ".$file_type.", Image          Size: ".ceil($file_size / 1024)." kb)</p>";
}

Thanks loz, for making me take off my blinders and realize that I just needed to do some simple troubleshooting. :-)