Solved

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

Posted on 2006-10-20
4
234 Views
Last Modified: 2012-05-05
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
Comment
Question by:mxgrogg
  • 2
4 Comments
 
LVL 1

Assisted Solution

by:imViper
imViper earned 75 total points
ID: 17776105
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
 

Author Comment

by:mxgrogg
ID: 17777061
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
 
LVL 17

Accepted Solution

by:
akshah123 earned 425 total points
ID: 17777405
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
 

Author Comment

by:mxgrogg
ID: 17782474
Perfect. good job!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now