Solved

Uploading Files and mkdir()

Posted on 2003-11-18
8
524 Views
Last Modified: 2013-12-12
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!
0
Comment
Question by:evco
  • 5
  • 3
8 Comments
 

Author Comment

by:evco
ID: 9775447
Or maybe a better way to go about creating a new dir and uploading files...
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9775929
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
0
 

Author Comment

by:evco
ID: 9775960
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!
0
 
LVL 13

Accepted Solution

by:
lozloz earned 500 total points
ID: 9776013
hmm, how are you using ftp? using fsockopen i suppose? this might be slower than using copy since you'd have to get the user to send the file to the server then have the server send the file to another server through ftp. it's much more complex than using common functions that are built into php and shouldn't be posing a problem. maybe you could try testing the same script on another server, or just take out the basics and try debugging by making folders, chmodding etc. and seeing where along the line these folders are created

loz
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:evco
ID: 9776050
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
  }
}
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9776168
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
0
 

Author Comment

by:evco
ID: 9776171
great thanks for all your help so far!!
0
 

Author Comment

by:evco
ID: 9804676
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. :-)
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now