Jump to content

sljaxon

Members
  • Posts

    11
  • Joined

  • Last visited

    Never

Posts posted by sljaxon

  1. How do I go about limiting uploads to multiple file types?

     

    Currently I am using..

     

    elseif ($_FILES['file']['type'] != 'image/gif')
    {
    echo "Your file must be an image.";
    }

     

    I want to try and limit it to .gif, .jpg, and .png but I was wondering how I make it limit to the multiple file types.

     

    Also I was wondering if it was possible to limit .gif files to non-animated .gifs.

     

    <?php
    if (($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/png"))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        die ("Error: " . $_FILES["file"]["error"]);
        }
      else
        {
        if (file_exists("uploads/" . $_FILES["file"]["name"]))
          {
          die ("File already exists");
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);
          }
        }
      }
    else
      {
      echo "GIF, JPEG, and PNG images only";
      }
    ?>
    

     

    This works for me every time.

    I don't think you can remove GIF animation unless you convert the GIF to another format with GD.

  2. Yes and No. You cannot have an standard link tag (<a href=...) that would include POST data with a basic tag.

    You could have something like this:

     

    <form action="postdata.php" method="post">
    <input type="hidden" name="pdata" value="this is my POST data">
    <input type="submit" value="My POST page">
    </form>
    

     

    That works, but it isn't a link. However, this example does the same thing, but uses javascript to use a link.

     

    <html>
    <head>
    <script language=javascript>
    function submitPostLink()
    {
    document.postlink.submit();
    }
    </script>
    </head>
    <body>
    <form action="postdata.php" name=postlink method="post">
    <input type="hidden" name="pdata" value="this is my POST data">
    </form>
    <a href=# onclick="submitPostLink()">My POST page</a>
    </body>
    </html>
    

     

    Should work.

     

    EDIT: HTML Syntax

  3. The hosts file is sort of annoying... This will work, or at least it did in my tests on Windows XP:

     

    Be sure it is this file: C:\windows\system32\drivers\etc\hosts

    No extension.

     

    Add this to the end:

    127.0.0.1       ebay.com
    127.0.0.1       www.ebay.com
    

     

    That works. Ebay IS a perfect example of a weakness of HOSTS: Type in one of the many Ebay subdomains, such as computers.ebay.com, and you are not sent to your apache server, you are sent to Ebay. However, some images and files may not be able to load. Other than that, this works.

  4. There are two ways I can figure out how to do this.

    1. Limit after upload to dimensions.

    2. Limit before upload to file size.

     

    First 1:

    Let's assume that you've stored the upload in "uploads/image.jpg" and want to limit the size to 640x480.

     

    $imageSize = getimagesize("uploads/image.jpg");
    if($imageSize[0] > 640 || $imageSize[1] > 480)
    {
    echo "invalid image: too big";
    unlink("uploads/image.jpg");
    }
    else
    {
    echo "Image OK.";
    }
    

     

    This is a suboptimal limitation method though, because the file has already been uploaded! To limit file size to 10KB, use code something like this in your upload script prior to storing the file.

     

    if($_FILES["imgFile"]["size"] < 10000)
    {
    echo "file too big";
    }
    else
    {
    echo "file OK";
    move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);
    }
    

     

    That should work.

  5. Note: I am assuming you want a PHP script to "read" the PDF to the user.

     

    Rarebit has the idea with the intermediary, which is needed, as mentioned later. To prevent direct access, this htaccess code will work:

     

    deny from all
    

     

    This should do exactly what you want.

    If you have mod_rewrite enabled, you may want to redirect users to the "reading" script instead of blocking them.

    If you want to do that, use this htaccess file:

     

    RewriteBase /
    RewriteRule (.*) read.php?file=$1 [QSA,L]
    

     

    One point - due to the way your script may be coded and the fact you are using PDFs, this code may not work.

     

    You cannot simply embed the PDF file in a HTML/PHP script, because that would be loaded client side by the PDF reader. Your script will need to read the PDF and then dynamically create a duplicate as rarebit's code does:

     

    Not that it blocks direct access, but is used as an intermediary (also logs...), just pass the name through as GET['item']... and store in a folder called 'dl/'...

    <?php
    
    function file_append($fn, $s)
    {
    $fp = fopen($fn, "a");	// use to append
    $written = 0;
    while ($written == 0)	// keep trying until lock is free
    {
    	if (flock($fp, LOCK_EX))
    	{
    		fwrite($fp, $s);
    		flock($fp, LOCK_UN);
    		$written = 1;
    	}
    }
    fclose($fp);
    
    return 0;
    }
    
    if(isset($_GET['item']))
    {
    $log = "log.txt";
    $dir = "dl/";
    $item = $_GET['item'];
    
    if ( (strcmp($item, "") != 0) && (file_exists($dir.$item)) )
    {
    	//	LOG
    	$rip = $_SERVER['REMOTE_ADDR'];
    	$time = $_SERVER['REQUEST_TIME'];
    	$method = $_SERVER['REQUEST_METHOD'];
    	$link = substr($_SERVER['REQUEST_URI'], 0, 128);
    	$agent = substr($_SERVER['HTTP_USER_AGENT'], 0, 128);
    
    	$s = $item." : ".$rip." : ".$time." : ".$method." : ".$link." : ".$agent."\n";
    
    	//$s = "hi\n";
    	file_append($dir.$log, $s);
    
    	//	REDIRECT TO FILE
    	header('Location: '.$dir.$item);
    }
    else
    {
    	print "<html><head><title>DOWNLOAD</title></head><body>REQUEST ERROR<br></body></html>";
    }
    }
    else
    {
    print "<html><head><title>ERROR</title></head><body>NO REQUEST FOUND!</body></html>";
    }
    
    ?>

     

    Basically it just use's 'header' to redirect!

     

    I'd be glad to answer any questions. You may also have special requirements, depending on exactly what you want to do with the PDF, such as advertisement insertion.

  6. I highly recommend reading this tutorial on mod_rewrite: http://www.easymodrewrite.com.

     

    For example, you can do exactly what you want to do with this htaccess code:

    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule (.*) p.php?user=$1 [QSA,L]
    

     

    This essentially tells Apache and mod_rewrite to first look if the file exists. If it does not, run it through p.php.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.