Solved

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

Posted on 2006-10-20
4
233 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 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

24 Experts available now in Live!

Get 1:1 Help Now