PHP - Folders And Files Permission
i have some questions on how folders and files permissions work. say i have users directories outside 'protected' as below..
Code: [Select] users -- usera -- docs -- userb -- docs protected i do not want user B who does not have the rights, to access anything in user A directories. also, i do not want any person to access the directories directory via url links. basically i just want users to be able to access their own directories, and no one else. how can it be done? thanks! Similar TutorialsHi, I have coded a site, whereby users can log in, and depending on their group, view all files within their group directory: Code: [Select] if ($handle = opendir("storage/Admin/$group")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo "<a target=\"_blank\" href=\"storage/Admin/$group/$file\">$file</a> <br />"; } } closedir($handle); } In this example, a user can click on their file, and it will let them download it, but if there is a folder (directory) in there, once clicked on, it will open a index of the directory. Is there a way instead, to let it open a custom page and list the files within it there? Hi Sorry if this is the wrong place for this but I'm totally stumped here and I know its a basic question but I'm very new to php and I need help :-) Basically all i would like to do is list the files and folders in the following directory alphabetically, I know i need to put them in an array etc but I don't know how and would really appreciate some help! So any help would be very gratefully received. Here's my code so far: <?php $sub = ($_GET['dir']); if (strstr($sub, "..")) {$sub = "";} $path = '../media/Documents'; $path = $path . "$sub"; $dh = opendir($path); $i=1; while (($file = readdir($dh)) !== false) { if(substr($file,0,1) != ".") { if (substr($file, -4, -3) =="."){ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. $file <br />"; }else{ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. <a href='?dir=$sub/$file'>$file</a><br />"; } $i++; } } closedir($dh); ?> Many thanks in advance! I'm pretty new to php and I'm furiously trying to get this code to list the folders/files alphabetically but to no avail! I'd really appreciate it if someone could take a look and guide me as to what to do, I'm pulling what little hair i have left out! I can currently see the folder contents but the order is all over the place. Thanks very much!!! -------------------------- <?php $sub = ($_GET['dir']); if (strstr($sub, "..")) {$sub = "";} $path = '../media/Movies'; $path = $path . "$sub"; $dh = opendir($path); $i=1; while (($file = readdir($dh)) !== false) { if(substr($file,0,1) != ".") { if (substr($file, -4, -3) =="."){ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. $file <br />"; }else{ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. <a href='?dir=$sub/$file'>$file</a><br />"; } $i++; } } closedir($dh); ?> -------------------------- hello, I have implemented a snippet that reads images in I create folders dynamically from admin and makes them see lightbox style .. the same works fine but it shows all images in all folders and not those relating to folder clicked .. The result you can see it he aires-restauro.org/beta/gallery2.php code: <?php $folder = opendir('gallery/'); echo "<gallery>"; while ($file = readdir($folder)) { if($file != '.' && $file != '..'){ echo "<album path='gallery/$file/' name='$file'>"; $subfolder="gallery/".$file."/"; $subfolderdir = opendir($subfolder); while ($subfile = readdir($subfolderdir)) { if($subfile != '.' && $subfile != '..'){ echo "<a href='$subfolder$subfile' class='pirobox_gall'>$file</a>"; } } echo "</album>"; } } echo "</gallery>"; ?> repeats the same directory as I would like to see that by clicking the directory do see the images of that directory sorry but I do not speak English well and I thank you in advance Hey guys, I'm very new to PHP so I apologize for any stupid mistakes or if I'm a little slow understanding things. I have put together a small website and I wanted to use a PHP script to display a list of files and folders (and sub folders). Basically a ghetto equivalent of an FTP page so I can apply CSS and make things look a little pretty. I have the following code I found from Google that will display all of the files and folders, not perfect but it will work for now! However I can't seem to work out the syntax to make them a hyperlink. Any help would be appreciated! <?php function getDirectory( $path = '.', $level = 0 ){ $ignore = array( 'cgi-bin', '.', '..' ); // Directories to ignore when listing output. Many hosts // will deny PHP access to the cgi-bin. $dh = @opendir( $path ); // Open the directory to the handle $dh while( false !== ( $file = readdir( $dh ) ) ){ // Loop through the directory if( !in_array( $file, $ignore ) ){ // Check that this file is not to be ignored $spaces = str_repeat( ' ', ( $level * 4 ) ); // Just to add spacing to the list, to better // show the directory tree. if( is_dir( "$path/$file" ) ){ // Its a directory, so we need to keep reading down... echo "<strong>$spaces $file</strong><br />"; getDirectory( "$path/$file", ($level+1) ); // Re-call this same function but on a new directory. // this is what makes function recursive. } else { echo "$spaces $file<br />"; // Just print out the filename } } } closedir( $dh ); // Close the directory handle } ?> I came accross this code on YouTube and it works great for listing the content of a folder that the .php file is in. However, how can I alter this code to list files of all folders and sub-folders? I thought plugging in an asterik might work on the "$path =" line, but it didn't. Code: [Select] <?php $path = "."; $handle = opendir($path); while ($file = readdir($handle)) { echo $file . "<br>"; } closedir($handle); ?> hi all i am looking for the quickest way to zip all files and folder of a website i am currently using the followning. <?php $date = date("F-j-Y-g-ia"); if(exec("cd /home/isd/public_html/bk/files;tar -cvpzf backup-$date.tar /home/isd/public_html")) { echo "done"; } ?> This works but was just wondering if anyone can give me and insight on a better solution?? Thanks I have use apache server with PHP installed on it,i created one directory through php coding it get created but it has permission ( drwxr-sr-x 3 www-data www-data 4096 Feb 26 04:54 Merchantid_30) my other directories has ubuntu:ubuntu permission, I want to write on my directory but www-data wont allow me to write on it,giving 777 permission through chmod is risky, can i create directory through php but its default permission is set to be ubuntu:ubuntu instead of www-data ? what changes i need to do in coding?
PHP is owner of image directory with "permissions 660." My script is: $imagePath = '../imageDirectory/'.gif'; $image = imagecreatefromstring(base64_decode($raw_image_data)); $rotate = imagerotate($image,-90,0); imagegif($rotate,'../imageDirectory/'.gif'); Is it because the imagerotate and imagegif functions need the execute requirements to be able to monkey with the image? Thank you.
Sub question (maybe more important than the first question): Thank you. Hi. I'm trying to make my folder management tool able to remove stuff. Problem is, I get permission denied.
I've checked the NFTS perms for the share and for the folder. It's also not read only. I logged into my other workstation as the service account the web server is using and opened a DOS window and did "rd \\server\share\folder" and it removed it no problem. It just fails in PHP. Any idea what I should be looking at? Thanks!
Edited by Strahan, 02 August 2014 - 05:06 PM. i want that when someone post some comment on post that don't insert in table directly before inserting i can check and after approving then insert in table for which what i doo ?any IDea about this I want to delete an image file from a folder using unlink. The code is : Code: [Select] $movie_id = $checkbox[$i]; $sql = "DELETE from movie_basic_details where `movie_id` = '$movie_id'"; $result = mysql_query($sql) or die(mysql_error()); $sql1 = "DELETE from movie_details where `movie_id` = '$movie_id'"; $result = mysql_query($sql1) or die(mysql_error()); $sql2 = "DELETE from movie_stars where `movie_id` = '$movie_id'"; $result = mysql_query($sql2) or die(mysql_error()); $query = "select movie_name from movie_basic_details where movie_id = '$movie_id'"; $res = mysql_query($query) or die(mysql_error()); $data = mysql_fetch_array($res); unlink("upload/movies/".$data['movie_name']); } The entries in the database have been deleted but only the image corresponds to that entry do not get deleted. The following warning comes up : Warning: unlink(upload/movies/) [function.unlink]: Permission denied in C:\wamp\www\ContentPanel\deletemovies.php on line 23 I have uploaded the files using $_FILES server variable and move_uploaded_file function. I am running this script on windows 7. Please Help. Hello I built a component for a non-profit organisation which want to use the 404page to promote several charities. In order to do that I want to place a file 'error.php' in the map 'templates/system'. I use the RENAME function for that. So far so good. On many sites it works fine, but on other sites I get an error on the rename function. The user-id and the group-id of the script I use are the same as the uid's of 'templates/system'. So there shouldn't be a problem. If I add public write to the filepermissions of 'templates/system'/ with Filezilla the error disappears. But I can't do that with the php chmod function, again because of too less priviliges. HELP, I can't find an explanation for that and that's driving me crazy. thanks in advance this script is supposed to save my $csvfile to a .sql file name $ouputfile into a folder called files/ $csvfile = $_FILES['file']['name']; //form from another folder $save = 1; $outputfile = "output.sql"; here is the save script if($save) { chmod($csvfile, 0777); if(!is_writable($outputfile)) { echo "File is not writable, check permissions.\n"; //when i execute this script, it goes in here } else { $file2 = fopen($outputfile,"w"); if(!$file2) { echo "Error writing to the output file.\n"; } else { fwrite($file2,$queries); fclose($file2); } } Im in the process of setting up a social media site , Just switched from basic hosting to Dedicated Server Everything was working till i switched . I have a basic join forum and email activation , On the register.php file it tell script to make a directory mkdir into member/folder like this below, But it will make the folder for the id of that user but will not let them upload any images?. At first it wouldnt even make a folder but i found out it was a permissions issue with my server i fixed it , im guessing the upload is also a permission's issue ? But the script automaticly make the members/"$id" folder and i dont know how to set permission for auto create folder that if i even have to ? so confused. (BEGGGGINGG !! FORRRRR HELPPPPPPPPPPPPPPPPPPPP! Im running a dedicated server with godaddy, On centOs w/ Plesk Panel 9.3.0 ------------------------------------------------------------------------------------------- $id = mysql_insert_id(); // Create directory(folder) to hold each user's files(pics, MP3s, etc.) mkdir("members/$id", 0755); Maybe never mind. Just noticed ssphpd doesn't have a group... ps. do I have the umask part right? Edit 2. No, it didn't help... I have two users (ssphpd and apache) which will need to write files to a given directory and its subdirectories. My intent was to set them up with ssphd's group and use the setgid to make all future files and directories inherit that group. Not sure, but I don't think I have this right yet as I indicate at the end of this post. [michael@bb5 ~]$ sudo useradd -r ssphpd [michael@bb5 ~]$ sudo usermod -aG wheel ssphpd [michael@bb5 ~]$ sudo usermod -aG wireshark ssphpd [michael@bb5 ~]$ sudo usermod -aG ssphpd apache [michael@bb5 ~]$ groups apache apache : apache ssphpd [michael@bb5 ~]$ groups ssphpd ssphpd : ssphpd wheel wireshark [michael@bb5 ~]$ [michael@bb5 ~]$ sudo chmod g+rwx -R /var/www/~/storage [michael@bb5 ~]$ sudo chown michael.ssphpd -R /var/www/~/storage [michael@bb5 ~]$ sudo chmod g+s /var/www/~/storage My script to write this files is below: public function setFile(array $file, string $nameAlias):void { $path=$this->getPath($nameAlias, true); $this->moveTo($file['tmp_name'], $path); } private function getPath(string $name, bool $create):string { //$name is 20 characters $p1=$this->uploadDirectory.'/'.substr($name,0,2); $p2=$p1.'/'.substr($name,2,3); $p3=$p2.'/'.substr($name,5,4); $old_umask = umask(0); if($create) { if(!file_exists($p1)){mkdir($p1, 0775);} if(!file_exists($p2)){mkdir($p2, 0775);} if(!file_exists($p3)){mkdir($p3, 0775);} } umask($old_umask); return $p3.'/'.substr($name,9); } private function moveTo(string $tmpname, string $path):void { syslog(LOG_ERR, "attempting to move $tmpname to $path"); $uid = posix_getuid(); syslog(LOG_ERR, "user PHP is running as: $uid - ".posix_getpwuid($uid)['name']); $gid = posix_getgid(); syslog(LOG_ERR, "group PHP is running as: $gid - ".posix_getpwuid($gid)['name']); $this->test($tmpname); $this->test(substr($path,0,-12)); syslog(LOG_ERR, move_uploaded_file($tmpname, $path)?'success':'failure'); } private function test(string $file):void { syslog(LOG_ERR, "$file is writable: ".(is_writable($file)?'yes':'no')); $uid=fileowner($file); syslog(LOG_ERR, "$file owner: $uid - ".posix_getpwuid($uid)['name']); $gid=filegroup($file); syslog(LOG_ERR, "$file group: $gid - ".posix_getpwuid($gid)['name']); $perms=fileperms($file); syslog(LOG_ERR, "$file permissions: $perms - ".$this->formatPerms($perms)); } However, as seen, not only can only apache write files, the newly created directories are displaying apache's group instead of ssphp's group. Any ideas? Jun 06 19:40:29 bb5.net Server[8485]: attempting to move /tmp/DL_TS_6n0kmr to /var/www/dev/storage/uploads/45/699/ed6c/d15fd6959b6 Jun 06 19:40:29 bb5.net Server[8485]: user PHP is running as: 989 - ssphpd Jun 06 19:40:29 bb5.net Server[8485]: group PHP is running as: 986 - Jun 06 19:40:29 bb5.net Server[8485]: /tmp/DL_TS_6n0kmr is writable: yes Jun 06 19:40:29 bb5.net Server[8485]: /tmp/DL_TS_6n0kmr owner: 989 - ssphpd Jun 06 19:40:29 bb5.net Server[8485]: /tmp/DL_TS_6n0kmr group: 986 - Jun 06 19:40:29 bb5.net Server[8485]: /tmp/DL_TS_6n0kmr permissions: 33152 - rrw------- Jun 06 19:40:29 bb5.net Server[8485]: /var/www/dev/storage/uploads/45/699/ed6c is writable: yes Jun 06 19:40:29 bb5.net Server[8485]: /var/www/dev/storage/uploads/45/699/ed6c owner: 989 - ssphpd Jun 06 19:40:29 bb5.net Server[8485]: /var/www/dev/storage/uploads/45/699/ed6c group: 986 - Jun 06 19:40:29 bb5.net Server[8485]: /var/www/dev/storage/uploads/45/699/ed6c permissions: 16893 - drwxrwxr-x Jun 06 19:40:29 bb5.net Server[8485]: failure Jun 06 19:40:32 bb5.net Api[6981]: attempting to move /tmp/phpy1CRQR to /var/www/dev/storage/uploads/ec/911/7ab7/c73065bff0b Jun 06 19:40:32 bb5.net Api[6981]: user PHP is running as: 48 - apache Jun 06 19:40:32 bb5.net Api[6981]: group PHP is running as: 48 - apache Jun 06 19:40:32 bb5.net Api[6981]: /tmp/phpy1CRQR is writable: yes Jun 06 19:40:32 bb5.net Api[6981]: /tmp/phpy1CRQR owner: 48 - apache Jun 06 19:40:32 bb5.net Api[6981]: /tmp/phpy1CRQR group: 48 - apache Jun 06 19:40:32 bb5.net Api[6981]: /tmp/phpy1CRQR permissions: 33152 - rrw------- Jun 06 19:40:32 bb5.net Api[6981]: /var/www/dev/storage/uploads/ec/911/7ab7 is writable: yes Jun 06 19:40:32 bb5.net Api[6981]: /var/www/dev/storage/uploads/ec/911/7ab7 owner: 48 - apache Jun 06 19:40:32 bb5.net Api[6981]: /var/www/dev/storage/uploads/ec/911/7ab7 group: 48 - apache Jun 06 19:40:32 bb5.net Api[6981]: /var/www/dev/storage/uploads/ec/911/7ab7 permissions: 16893 - drwxrwxr-x Jun 06 19:40:32 bb5.net Api[6981]: success Edited June 6, 2019 by NotionCommotion Hi all, we are new bee to php, I am developing a project that has config and other sub folders under admin directory ROOT/ADMIN, ROOT/ADMIN/FOLDER1,2,3... how to manage redirection from one file to another folder. Im using XAMPP at the moment and my applicatin im making is in a folder in the htdocs folder where all my files are read from. I notice when im php scripting if I put any files into another folder with in my main folder it wont load the page. so when I want to load a .php file from a script do i need to add more then just go to this .php file? I tried adding folder/*****.php but that didnt work I want to use multiple folders so its more organized hope someone can help me out with this Hey guys i am making a php application and i have a feature where it allows members to upload images. If there a way to secure a folder to only be allowed access when a member is logged in and not someone accessing the folder and downloading images. Stuped question i know would it be better to store the images in the database as BLOB? but then again could make the database big. Thanks I am trying to run the following code to upload a file. (WAMP using Win XP). Once I select the file and click submit I get the error: "Forbidden... You don't have permission to access /< on this server." with URL http://localhost/<?=$PHP_SELF?> Code is below: <form action="<?=$PHP_SELF?>" method="post" enctype="multipart/form-data"> <br/><br/> Choose a file to upload:<br/> <input type="file" name="upload_file"> <br/> <input type="submit" name="submit" value="submit"> </form> TIA |