PDA

View Full Version : Permission issues I think


Exvitermini
06-03-2008, 09:22 PM
hey guys. i need help wit my code.
so wut im coding is a little script to mass rename a bunch of files of a certain extension. so i got it all coded but it will only work for files that reside in the same directory as the script (in ./)
i want to be able to rename files in any directory within the computer's drives or network drive folders. is dis possible?
I wrapped some error check is_writable() around my code to wuts happening. it seems to be file permission issues i think.
i don't know how to fix this issue.

when i put the .jpg files into say c:\jpgs folder, my script will display the following because i coded the error.
but it seems to be permission. I cant seem to write enable at all. Im also using windows xp and most of my work place uses the same OS.

BMW M3.jpg --------> Error: file is not writable. Check permissions!
carina07_hst_big.jpg --------> Error: file is not writable. Check permissions!
darkriver_ruiz_big.jpg --------> Error: file is not writable. Check permissions!


but if put the same files in the folder with the script in,
it will display the file names and the textboxes and stuff and i can rename them.

I will attach my full code with this.

Please i hope to get this issue resolved.
Please and Thank you.


<?php
//working directory
//NOTE: No '\' at the end
$dir_path = "C:/Documents and Settings/cjayawardana/Desktop/CDJ";
//open the directory
$dhandle = @opendir($dir_path) or die("<font color=\"red\"><b>Error: Unable to open $dir_path</b></font>");
$files = array(); // define an array to hold the files
$renames = array(); //rename file names
$extension = "jpg"; //file extension
$counter = 1; //counter

/////////////////////////////////////////////////////////////////
//////// Start Rename ////////
/////////////////////////////////////////////////////////////////
//after Submit button is pressed in the form, page refreshes and goes into
//this if statement
if($rename == 1)
{
//import all form elements as variables
@import_request_variables("gpc");

//explode the string of file names into an array (undoing the implode we did earlier)
$file_array = explode(",", $fileArray);

//create the array of names to rename to from the POST variables
for($i = 1; $i <= $numTxtBoxes; $i++)
{
//create the variable name to extract from _POST
$str = "textbox".$i;
//$$str is a Variable variable.
$rename_array[] = $$str . "." . $extension;
}

//Display the array of files and file names in a table
echo "<table border=\"1\">";
echo "<tr>";
echo "<th>Files in the directory</th>";
echo "<th>Rename files to</th>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<pre>";
print_r ($file_array); //print the file array
echo "</pre>";
echo "</td>";
echo "<td>";
echo "<pre>";
print_r ($rename_array); //print the rename array
echo "</pre>";
echo "</td>";
echo "</tr>";
echo "</table>";

//rename the files
for($i = 0; $i < $numTxtBoxes; $i++)
{
//if not renamed, print error
if( !(rename($file_array[$i], $rename_array[$i])) )
{
echo "<font color=\"red\">Error: " . $file_array[$i] . " could not be renamed to " . $rename_array[$i] . "</font>";
}
}

echo "<input type=\"button\" value=\"Go Back\" onclick=\"history.go(-1);\"";

exit(0);
}
///////////////////////////////////////////////////////////////
//////// End Rename ////////
///////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
//////// Start Form ////////
//////////////////////////////////////////////////////////////
echo "<h3>Working directory: $dir_path</h3>";
echo "<h3>File extension: $extension</h3>";
echo "<form name=\"form$counter\" action=\"$_SERVER[PHP_SELF]?rename=1\" method=\"post\">";
if ($dhandle)
{
echo "<table>";
echo "<tr>";
echo "<th>File Name</th>";
echo "<th>--------></th>";
echo "<th>Rename to</th>";
echo "</tr>";

// loop through all of the files
while (false !== ($fname = readdir($dhandle)))
{
// if the file is not this file, and does not start with a '.' or '..',
// then store it for later display ($fname != basename($_SERVER['PHP_SELF']))
if ( ($fname != '.') && ($fname != '..') && (!is_dir($fname)) )
{
//match the extension of the file
if(preg_match("/($extension)/i", $fname))
{
//$full_addr = $dir_path . "\\" . $fname;
//echo $full_addr;
//chmod($full_addr, 0777);
//chown($full_addr, "cjayawardana");

//add the file name to array only if the file is write enabled.
//if not echo error
if( is_writable($fname) )
{
// store the filename
$files[] = (is_dir( "./$fname" )) ? "(Dir) {$fname}" : $fname;

//display the files with the rename textbox
echo "<tr>";
echo "<td>";
echo "$fname";
echo "</td>";
echo "<td>";
echo "-------->";
echo "</td>";
echo "<td>";
echo "<input type=\"text\" name=\"textbox$counter\" size=\"50\" />";
echo "</td>";
echo "</tr>";

//increase counter
$counter++;
}
else
{
//display file name and error
echo "<tr>";
echo "<td>";
echo "<font color=\"red\"><i>$fname</i></font>";
echo "</td>";
echo "<td>";
echo "<font color=\"red\">--------></font>";
echo "</td>";
echo "<td>";
echo "<font color=\"red\"><i>Error: file is not writable. Check permissions!</i></font>";
echo "</td>";
echo "</tr>";
}
}
}
}

if( sizeof($files) == 0 )
{
echo "<font color=\"red\"><i>No files in this directory can be renamed of the extension</i> <b>.$extension</b></font>";
}

echo "</table>";

//implode the array into a string
$file_names = implode(",", $files);
//because after the last file has been read, the counter increases an extra one. have to minus 1 to get number of elements
$counter--;

//read only textfields.
//1) file array as a string
//2) number of elements in the file array
echo "<table>";
echo "<tr>";
echo "<td>";
echo "File array as string (Delim = ,)";
echo "</td>";
echo "<td>";
echo "<input type=\"text\" name=\"fileArray\" size=\"100\" value=\"$file_names\" readonly />";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "Array element counter";
echo "</td>";
echo "<td>";
echo "<input type=\"text\" name=\"numTxtBoxes\" size=\"100\" value=\"" . sizeof($files) . "\" readonly/>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<input type=\"submit\" name=\"submit\" value=\"Rename Files\"/>";
echo "</td>";
echo "</tr>";
echo "</table>";

// close the directory
closedir($dhandle);
}
echo "</form>";
/////////////////////////////////////////////////////////////
//////// End Form ////////
/////////////////////////////////////////////////////////////
?>

rfresh
06-04-2008, 01:30 AM
To be able to change any file in any folder your script will have to run with root privilages otherwise it will fail outside of its own folder. How much management control over your web server do you have? Are you hosting your own server or is someone else hosting it for you?

Exvitermini
06-04-2008, 05:44 AM
i have all the control over it. im running my own just for development and then after everything has been tested and runs good, it goes into a server that is out of my hands.
rite now im running this script on my own server.
so how do i run my script wit root privilages?

Exvitermini
06-04-2008, 08:22 PM
anyone? how do i run my script wit root privilages?

tomws
06-05-2008, 05:26 AM
You can't if you're using a browser to hit the script. It's being run as the web server user (apache, www-data, httpd, etc). And anyway, you want Administrator on Windows instead of root. Technically, you can screw with permissions to allow all kinds of things to happen, but someone will have to "help" you with that.

And rfresh is only partially correct. The server will allow you to hit files within the local site, meaning anything within the top level directory for the site. You're trying to access your Windows desktop, which is probably not the docroot for your site.

Exvitermini
06-05-2008, 01:50 PM
oh icic. ya desktop is not my docroot.
well anyways i'll have to prolly make some sorta app instead.
thanks for ur help