CHMOD of untared (via php-script) files on webspace

4 replies
Does someone know how to modify the following function so it is possible to set the CHMOD for the extracted files? Actually I can set the folder permissions but do not know how to change the file permissions - actually all untared files have 0777 which is really bad,
I would like to set them to 0644

Here comes the function:

function untar($tarfile, $outdir="./", $chmod = null) {
global $log, $LANG, $lang;
$TarSize = filesize($tarfile);
$TarSizeEnd = $TarSize - 1024;
if($outdir!="" && !file_exists($outdir)) {
@mkdir($outdir,0755,true);
}
$thandle = fopen($tarfile, "r");
while (ftell($thandle) < $TarSizeEnd) {
$FileName = fread($thandle,100);
$FileName = $outdir.trim($FileName);
$FileMode = trim(fread($thandle,8));
if($chmod===null) {
$FileCHMOD = octdec("0".substr($FileMode,-3));
}
if($chmod!==null) {
$FileCHMOD = $chmod;
}
$OwnerID = trim(fread($thandle,8));
$GroupID = trim(fread($thandle,8));
$FileSize = octdec(trim(fread($thandle,12)));
$LastEdit = trim(fread($thandle,12));
$Checksum = trim(fread($thandle,8));
$FileType = trim(fread($thandle,1));
$LinkedFile = trim(fread($thandle,100));
fseek($thandle,255,SEEK_CUR);
if($FileType=="0") {
if ($FileSize>0) {
$FileContent = fread($thandle,$FileSize);
} else {
$FileContent = '';
}
}
if($FileType=="1") {
$FileContent = null;
}
if($FileType=="2") {
$FileContent = null;
}
if($FileType=="5") {
$FileContent = null;
}
if($FileType=="0") {
if (!file_exists(dirname($FileName))) {
@mkdir(dirname($FileName), 0755, true);
}
$subhandle = fopen($FileName, "w");
$log .= $LANG[$lang]['L_FILE'].$FileName."\n";
fwrite($subhandle,$FileContent,$FileSize);
fclose($subhandle);
chmod($FileName,$FileCHMOD);
}
if($FileType=="1") {
//link($FileName,$LinkedFile);
$log .= $FileName." link!\n";
}
if($FileType=="2") {
//symlink($LinkedFile,$FileName);
$log .= $FileName." symlink!\n";
}
if($FileType=="5") {
$log .= $LANG[$lang]['L_DIR'].$FileName."\n";
@mkdir($FileName, 0755, true);
}
//touch($FileName,$LastEdit);
if($FileType=="0") {
$CheckSize = $FileSize == 0 ? 0 : 512;
while ($CheckSize<$FileSize) {
if($CheckSize<$FileSize) {
$CheckSize = $CheckSize + 512;
}
}
$SeekSize = $CheckSize - $FileSize;
fseek($thandle,$SeekSize,SEEK_CUR);
}
}
fclose($thandle);
return true;
}



Any help is very much appreciated.
#chmod #files #phpscript #untared #webspace
  • Profile picture of the author KirkMcD
    Are you passsing '0644' in the third variable when you call the function?
    {{ DiscussionBoard.errors[4400164].message }}
    • Profile picture of the author toskana
      I just got a solution for this:

      Insteady of:

      chmod($FileName,$FileCHMOD);

      we now use:

      chmod($FileName,0755);
      {{ DiscussionBoard.errors[4400887].message }}
      • Profile picture of the author BillBert
        Originally Posted by toskana View Post

        I just got a solution for this:

        Insteady of:

        chmod(,);

        we now use:

        chmod(,0755);

        Rather than hard code the permissions, it would be safer to move upstream and set the variable to the value that you require. It could save you from developing a bug or issues in the future when updating. Just my two cents.
        Signature
        Do You Need a Cost Effective way of reaching out to your offline clients or prospects?

        Feel free to contact me directly and I will give you the details!
        {{ DiscussionBoard.errors[4404045].message }}
      • Profile picture of the author bestfriend
        Hi toskana,

        In your function you've octdec on the mod value if chmod variable didn't passed, but you didn't use octdec on the mod if chmod variable passed on function call.

        When this changed;

        if($chmod!==null) {
        $FileCHMOD = $chmod;
        }

        to this;

        if($chmod!==null) {
        $FileCHMOD = octdec( $chmod );
        }

        you don't need to hard code the permission mode in the function anymore.
        {{ DiscussionBoard.errors[4405396].message }}

Trending Topics