chmod() not working!

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
remvs
Forum Newbie
Posts: 4
Joined: Mon Aug 15, 2005 3:46 pm

chmod() not working!

Post by remvs »

Hi all,

I was working on a script for uploading files. I kept running into troubles after i migrated everything from my local testserver to the webserver of my isp. Finally i decided to start a little test-project to get to the problem.

After a while i found out that the directory i create doest get the correct chmod-mode.
I can check this when i log into my domain with the admin page-thingy. i see that the directory created has neither READ nor WRITE enabled

here is what happens:

1. I run the script for the first time
2. the directory 'upload_files' is created and mode is set (in code at least)
3. afterwards no file is uploaded
4. when i check the mode from my ISP-admin-page READ nor WRITE is checked.
5. so no file is uploaded


my thoughts:
file modes can be set for Owner, Group and Global; but the admin page only shows two checkboxes; READ and WRITE. i don't know which read/write this is, so i tried a couple of chmod-variants. but no luck.

can anyone please help?!
im not sure what kind of server (unix or windows) my pages are running, does that have anything to do with it?

here's the code


Code: Select all

<?php
error_reporting(E_ALL);

echo $site_name = $_SERVER['HTTP_HOST'];
echo $url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
echo $url_this =  "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
echo $upload_dir = "upload_files/";
echo $upload_url = $url_dir."/upload_files/";
echo  $message ="";

// DEBUGGING
//var_dump($_FILES);
//$a=stat($upload_dir);
//var_dump($a);
//echo "<br>";
//echo $a[mode];


//create upload_files directory if not exist
//If it does not work, create on your own and change permission.
if (!is_dir("upload_files")) {
mkdir("upload_files",0777);
	die ("upload_files directory doesn't exist, now it's created'");
}

if ($_FILES['userfile']) {
	$message = do_upload($upload_dir, $upload_url);
	
}
else {
	$message = "Invalid File Specified.";
}

print $message;

function do_upload($upload_dir, $upload_url) {

	$temp_name = $_FILES['userfile']['tmp_name'];
	$file_name = $_FILES['userfile']['name'];
	$file_type = $_FILES['userfile']['type'];
	$file_size = $_FILES['userfile']['size'];
	$result    = $_FILES['userfile']['error'];
	$file_url  = $upload_url.$file_name;
	$file_path = $upload_dir.$file_name;

	//File Name Check
    if ( $file_name =="") {
    	$message = "Invalid File Name Specified";
    	return $message;
    }
    //File Size Check
    else if ( $file_size > 500000) {
        $message = "The file size is over 500K.";
        return $message;
    }
    //File Type Check
    else if ( $file_type == "text/plain" ) {
        $message = "Sorry, You cannot upload any script file" ;
        return $message;
    }

    $result  =  move_uploaded_file($temp_name, $file_path);
    $message = ($result)?"File url <a href=$file_url>$file_url</a>" :
    	      "Somthing is wrong with uploading a file.";

    return $message;
}
?>
<form name="upload" id="upload" action="<?php echo $PHP_SELF ?>"  ENCTYPE="multipart/form-data" method="post">
  Upload Image<input type="file" id="userfile" name="userfile">
  <input type="submit" name="upload" value="Upload">
</form>

feyd | please use

Code: Select all

tags when posting large blocks of php code.[/color]
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

try using chmod() right after you make the directory, if you get any safe mode errors then do a ftp_connect and then use the ftp commands to chmod the directory and that will be able to bypass all the safe mode problems, but thats only if chmod() does not work.
remvs
Forum Newbie
Posts: 4
Joined: Mon Aug 15, 2005 3:46 pm

Post by remvs »

in addition:

when i try to find out what the mode of the directory is i tried this

Code: Select all

$a=stat($upload_dir); 
  echo $a[mode];
everytime i get this result:
[2]=> int(16895)

if i manually change the mode, doesnt matter, i allways get this 16895 (40777 in oct.)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

try what i said and see what happens
remvs
Forum Newbie
Posts: 4
Joined: Mon Aug 15, 2005 3:46 pm

Post by remvs »

shiznatix,

thanx for the very quick reply!
i tried the chmod(), but no success :(

could you please explain what to do with ftp_connect ?
I havent worked with that yet, so, please some directives

Thanks alot!!

Remvs
Holland
shiznatix wrote:try using chmod() right after you make the directory, if you get any safe mode errors then do a ftp_connect and then use the ftp commands to chmod the directory and that will be able to bypass all the safe mode problems, but thats only if chmod() does not work.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

ftp_connect
ftp_chmod
ftp_close

you MAY but probebly not have to use

ftp_chdir
the last one here changes the directory that php is looking in but you shouldnt have to use it


the rest of those are perdy easy to use, if you dont understand the manual then post and i will give you an example no problem
remvs
Forum Newbie
Posts: 4
Joined: Mon Aug 15, 2005 3:46 pm

Post by remvs »

im back

Not working !! :( i cant get the ftp thing to work
i dunno hgat im doing wrong here!

Code: Select all

if (!is_dir("upload_files"))
{
  mkdir("upload_files",0777);
  //chmod("upload_files",0777);
}

  $ftpcon = "ftp.armadasoftware.nl";
  $dir = 'wwwroot/b/';
  $file ='upload_files';
  
  // set up basic connection
  $conn_id = ftp_connect($ftpcon);
  //  login with username and password
  $login_result = ftp_login($conn_id, '****', '****');
  ftp_chdir($conn_id,$dir);
  echo ftp_pwd($conn_id);
  if (ftp_chmod($conn_id, 0777,$file) !== false)
  {
   echo "$file chmoded successfully to xxx\n";
  }
   else
  {
    echo "could not chmod $file\n";
  }

	die ("upload_files directory doesn't exist, now it's created'");
	ftp_close($ftpcon);

help..... :(

JCART | Please use

Code: Select all

tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Post Reply