mkdir() and move_uploaded_file()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vanceibz
Forum Newbie
Posts: 10
Joined: Sat Jan 14, 2006 6:25 pm

mkdir() and move_uploaded_file()

Post by vanceibz »

hello everyone,
I am fairly new to these functions, I am trying to create a folder on my server using the mkdir function using an item name from the database which works fine, but i have trouble using move_uploaded_file() to then copy a php file from a form to the newly created folder, I have done some reading and i think the problem is because i have created the folder from an admin page , the user who created the folder user is 30 , which creates the problem.

How can i fix the problem , is it possible to create a folder from the admin page and then copy a file to the folder?

thanks for any help in advance. code i have pasted below.

vance

Code: Select all

function insert_template($id) {



$connection = mysql_connect($Host, $UserName, $PassWord);
mysql_select_db($DataBase);


$Room = mysql_query("select * from item_desc d,personal p,item i where d.item_id = i.item_id AND i.personal_id = p.personal_id AND i.personal_id = $id AND d.language_id = 1");
$room = mysql_fetch_assoc($Room);

if(is_dir("../personal/". $room["item_name"] ."/",0777) == false)

  {
 
  mkdir("../personal/". $room["item_name"] ."/");
  chmod("../personal/". $room["item_name"] ."/",0777);  
  echo"". $room["item_name"] ."";
  
  

  }

if(is_uploaded_file($_FILES["file"]["tmp_name"]))
  {
  $file_handle = fopen($_FILES["file"]["tmp_name"], "rb");
  $file = addslashes(fread($file_handle, filesize($_FILES["file"]["tmp_name"])));
     $Update = "Update personal set file = '" . $file . "' where personal_id = " . $room["personal_id"];
  mysql_query($Update);
      
  move_uploaded_file($_FILES["file"]["tmp_name"], "../personal/". $room["item_name"] ."/home_". $room["personal_id"] .".jpg");  
   echo"yes";
  }
 

mysql_close($connection);


}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Use FTP to chmod the folder to 0777.
vanceibz
Forum Newbie
Posts: 10
Joined: Sat Jan 14, 2006 6:25 pm

Post by vanceibz »

hi, thats fine the folder is already 0777, there are no permission problems. I think the problem is because i created the folder through the admin website and not directly from an ftp program. Usually i get when i create folders through ftp i get owner 711, when i create using the script i get owner 30. so i think it doesnt let me move a file to the dynamic created folder (owner 711) . thanks for the advice
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Could be wrong but it might not be able to move the file as appears the script is trying to move a file which has an open file pointer. (i.e. fopen but no fclose, unless I missed it?)

Personally, I'd also throw a bit more error checking in there too.
vanceibz
Forum Newbie
Posts: 10
Joined: Sat Jan 14, 2006 6:25 pm

Post by vanceibz »

that could be, mm i dont know how to close it though , how would that be done? I have used the script before to upload images to folders that i created using ftp and there were no problems... also error checking , what code can i add for that... thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

vanceibz wrote:that could be, mm i dont know how to close it though , how would that be done?
fclose($file_handle);
vanceibz
Forum Newbie
Posts: 10
Joined: Sat Jan 14, 2006 6:25 pm

Post by vanceibz »

YES!!!! thats was it , using $file_handle = fclose($_FILES["file"]["tmp_name"], "rb"); to close it worked fine!!! greate bruv you helped me alot of time and stress, thanks so much

thanks again
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

vanceibz wrote:YES!!!! thats was it , using $file_handle = fclose($_FILES["file"]["tmp_name"], "rb"); to close it worked fine!!! greate bruv you helped me alot of time and stress, thanks so much

thanks again
/me Passes thanks on to redmonkey ;)
vanceibz
Forum Newbie
Posts: 10
Joined: Sat Jan 14, 2006 6:25 pm

Post by vanceibz »

DAmn, no mate it didnt work , i thought it did! but i was looking at the wrong folder, :) if i change the script to move the file to a folder that i created using ftp it works but not to a dynamically created one.
vanceibz
Forum Newbie
Posts: 10
Joined: Sat Jan 14, 2006 6:25 pm

Post by vanceibz »

this works fine to move an image to folder personal created from ftp (owner 711)

Code: Select all

if(is_dir("../personal/",0777) == false)

  {
 
  mkdir("../personal/");
  chmod("../personal/",0777);  
  echo"". $room["item_name"] ."";
  
  

  }

if(is_uploaded_file($_FILES["file"]["tmp_name"]))
  {
  $file_handle = fopen($_FILES["file"]["tmp_name"], "rb");
  $file = addslashes(fread($file_handle, filesize($_FILES["file"]["tmp_name"])));
     $Update = "Update personal set file = '" . $file . "' where personal_id = " . $room["personal_id"];
  mysql_query($Update);
      
  move_uploaded_file($_FILES["file"]["tmp_name"], "../personal/home_". $room["personal_id"] .".jpg");  
   echo"yes";
   fclose($file_handle);
  }
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I notice that we're trying to reassign file_handle. Don't.

fclose($file_handle) like I said... fclose() works on the pointer, not the file itself.

EDIT | Just saw you last post while I was typing this..., aplogies I can see it's been changed now :)
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

It may not be fclose, that's just an inital thought. I notice however that you have placed fclose after the move_uploaded call?

You may also try looking at umask(). Have you confirmed that your script is actually setting the correct permissions via chmod?
vanceibz
Forum Newbie
Posts: 10
Joined: Sat Jan 14, 2006 6:25 pm

Post by vanceibz »

yea, i have moved fclose() i hope its looks ok there, the permissions for the created folder are 777 , the strange thing is owner 30, which i think is the problem maybe i cant create a new file with owner 30 in a folder with owner 30, dont know??¿

forgive me for my little php knowledge...

Code: Select all

if(is_dir("../personal/". $room["item_name"] ."/",0777) == false)

  {
 
  mkdir("../personal/". $room["item_name"] ."/");
  chmod("../personal/". $room["item_name"] ."/",0777);  
  echo"". $room["item_name"] ."";
  
  

  }

if(is_uploaded_file($_FILES["file"]["tmp_name"]))
  {
  $file_handle = fopen($_FILES["file"]["tmp_name"], "rb");
  $file = addslashes(fread($file_handle, filesize($_FILES["file"]["tmp_name"])));
     $Update = "Update personal set file = '" . $file . "' where personal_id = " . $room["personal_id"];
  mysql_query($Update);
      
  move_uploaded_file($_FILES["file"]["tmp_name"], "../personal/". $room["item_name"] ."/home_". $room["personal_id"] .".jpg");  
   echo"yes";
   
  }
 
fclose($file_handle);
mysql_close($connection);
error_reporting(E_ALL);
}
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

It would seem a bit odd that a user can create a directory but not write to it.

You could try the code I posted here there are two examples at the bottom of the post. The first example shows how to use it for making directories and moving files, the second showws an example of working with uploaded files.

It does seem to be more of a permissions thing, and FTP may be an easier solution.
vanceibz
Forum Newbie
Posts: 10
Joined: Sat Jan 14, 2006 6:25 pm

Post by vanceibz »

thanks, i will have a read , looks quite promising the code you created, maybe its easier with ftp will have a good try, take care
Post Reply