• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

PHP: code to set permissions to 777 on pictures uploaded using this HTML / PHP script

Hello all.

I have this page, which is to upload pictures to my server, but it uses a basic html form type=file kind of upload which is run by php.  The problem is, that when the picture gets uploaded into the 777 directory, it always gets uploaded as a 600 permission.  So now, I can't see the photo online!.

I got very frustrated and remembered that "when you're frustrated, ask EE!". so here I am.  

I know you can change the file permissions using php, but I don't know where in this script I would put it.  This script is so php - html integrated, that I wanted your ideas on what I would do to make the file that is uploaded through this script default to 777.

What do I write, and where (perhaps many places) do I put it?

Here is the link to the text of the file: <a href="http://www.aunthat.com/nixedia_test/htdocs/photo_upload.txt">Here</a> or
http://www.aunthat.com/nixedia_test/htdocs/photo_upload.txt

Points awarded due to completeness of the answer and ease of undersanding your answer.  
Thanks all!
-Mxgrogg-

0
mxgrogg
Asked:
mxgrogg
  • 2
2 Solutions
 
imViperCommented:
Hi,
You can make use of the PHP function chmod to accomplish this. Below is an example ..

<?php
// Read and write for owner, nothing for everybody else
chmod("/somedir/somefile", 0600);

// Read and write for owner, read for everybody else
chmod("/somedir/somefile", 0644);

// Everything for owner, read and execute for others
chmod("/somedir/somefile", 0755);

// Everything for owner, read and execute for owner's group
chmod("/somedir/somefile", 0750);
?>

Now, in your code where ever you are moving the uploaded temp file to a permanent directory, use the chmod function and you should be good to go.

Let's say, in your code $filename holds the filename+path, then you can do like this.. :

chmod ("$filename", 0777); and that's it.

Give it a try and post your feedback!
0
 
mxgroggAuthor Commented:
I am somewhat new to this. Could you look in my code and see where you would put this.  give me the line numbers please.
with the ifthen statements, it may need to be put in many locations. please identify them.
-Mxgrogg-
0
 
akshah123Commented:
try following ...

<?php
      if(isset($_POST["ID"]))
            $id = $_POST["ID"];
      else
            $id = $_GET["ID"];
            
      if(isset($_POST["form"]))
            $form = $_POST["form"];
      else
            $form = $_GET["form"];
      $msg = "";
      if(isset($_POST["Upload"]))
      {
            
         $uploaddir = $_POST['UploadDir']."/";
      
            for($i=1,$k=1;$i<=10;$i++)
            {                  
                  $filename=$k."_".$_FILES['image']['name'];
                  if(file_exists($uploaddir.$filename))
                  {
                        $k++;
                  }
                  else
                  {
                        break;
                  }
            }
            if($k>10)
            {
                  $msg = "3";
            }
            elseif($_FILES['image']['type']=="image/gif" || $_FILES['image']['type']=="image/jpeg" || $_FILES['image']['type']=="image/jpg" || $_FILES['image']['type']=="image/pjpeg")
            {
                  $uploadfile = $uploaddir . $filename;
                  if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile))
                  {
                        chmod($uploadfile, 0777);
                        $msg = 1;
                  }
                  else
                     echo "Possible file upload attack!\n";
            }
            else
            {
                  $msg = "2";
            }
            ob_clean();
      }

?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>photo upload script</title>
<style type="text/css">
<!--
body {
      margin-left: 0px;
      margin-top: 0px;
}
-->
</style>
<link href="css/style.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.style1 {color: #000000}
-->
</style>
<script language="javascript">
function check()
{
if(document.form1.image.value=="")
{
alert("Please select any Images for this");
return false;
}

}
function UpDateOpener(form, id, value)
{
      objectName = id;
      //alert(objectName);
      //alert(form);
      //alert(value);
      opener.document.getElementById(objectName).value = value;
      window.close()
}
</script>
</head>
<body bgcolor="#D6E8CE">
<table width="100%"  border="0" cellpadding="0" cellspacing="0" bgcolor="#D6E8CE">
  <tr>
    <td height="64" valign="top">&nbsp;</td>
  </tr>
  <?php if($msg!="") { ?>
  <tr>
    <td valign="top">
      <?php if($msg=="1") { ?>
      <table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
          <td height="57" align="center" class="smallBlackBold">The image has
            been successfully uploaded
            <script language="javascript">
                  //UpDateOpener('<? echo $form ?>','<? echo $id ?>', '<? echo $filename ?>');
            </script>
          <p>
            <input name="Button" type="button" class="btn" onClick="UpDateOpener('<? echo $form ?>','<? echo $id ?>', '<? echo $filename ?>')" value="Close Window">
          </p></td>
      </tr>
    </table>
      <?php } elseif($msg==2) { ?>
      <table width="100%"  border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td height="57" align="center" class="ErrorSmallRed">Only GIF and JPEG files are allowed.
              <p>
                <input name="Button" type="button" class="btn" onClick="javascript:history.go(-1);" value="<< Back to upload">
            </p></td>
        </tr>
      </table>
        <?php } elseif($msg==3) { ?>
      <table width="100%"  border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td height="57" align="center" class="ErrorSmallRed">A image with the
            same name already exists<br>Try to use another name.
            <p>
                <input name="Button" type="button" class="btn" onClick="javascript:history.go(-1);" value="<< Back to upload">
            </p></td>
        </tr>
      </table>
        <?php } ?>
        </td>
  </tr>
  <?php } else { ?>
  <tr>
    <td valign="top"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <form name="form1" method="post" action="" enctype="multipart/form-data" onSubmit="return check();">
      <tr class="smallBlackBold">
        <td width="17%">&nbsp;</td>
            <td width="28%">Select a file to upload</td>
        <td width="6%">:</td>
        <td width="42%"><input name="image" type="file" class="textbox" size="30" onChange="document.form1.filename.value=this.value;" id="Image"></td>
        <td width="7%">&nbsp;</td>
      </tr>
      <tr class="smallBlackNormal">
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td><input name="filename" type="hidden" id="filename"></td>
        <td>&nbsp;</td>
      </tr>
      <tr class="smallBlackNormal">
        <td>&nbsp;</td>
        <td><input name="Upload" type="hidden" id="Upload" value="Y">
          <input name="ID" type="hidden" id="ID" value="<? echo $_GET["ID"]  ?>">
              <input name="UploadDir" type="hidden" id="UploadDir" value="<? echo $_GET["UploadDir"]  ?>">
              <input name="form" type="hidden" id="form" value="<? echo $form ?>"></td>
        <td>&nbsp;</td>
        <td><input name="Submit" type="submit" class="btn" value="Upload" ></td>
        <td>&nbsp;</td>
      </tr>
        </form>
    </table></td>
  </tr>
  <?php } ?>
  <tr>
    <td valign="top">&nbsp;</td>
  </tr>
</table>
</body>
</html>

0
 
mxgroggAuthor Commented:
Perfect. good job!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now