Skip to content

Instantly share code, notes, and snippets.

@zvineyard
Created August 30, 2012 15:29
Show Gist options
  • Select an option

  • Save zvineyard/3530917 to your computer and use it in GitHub Desktop.

Select an option

Save zvineyard/3530917 to your computer and use it in GitHub Desktop.

Revisions

  1. zvineyard revised this gist Aug 30, 2012. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions php
    Original file line number Diff line number Diff line change
    @@ -10,15 +10,14 @@ if (isset($_POST['submit']))
    $filesize = $_FILES["file"]["size"];
    $allowed_file_types = array('.doc','.docx','.rtf','.pdf');

    if (in_array($file_ext,$allowed_file_types) && ($filesize < 200000))
    if (in_array($file_ext,$allowed_file_types) && ($filesize < 200000))
    {
    // Rename file
    $newfilename = md5($file_basename) . $file_ext;
    if (file_exists("upload/" . $newfilename))
    {
    // file already exists error
    echo "You have already uloaded this file.";

    echo "You have already uploaded this file.";
    }
    else
    {
    @@ -31,7 +30,7 @@ if (isset($_POST['submit']))
    // file selection error
    echo "Please select a file to upload.";
    }
    elseif ($filesize &gt; 200000)
    elseif ($filesize > 200000)
    {
    // file size error
    echo "The file you are trying to upload is too large.";
  2. zvineyard revised this gist Aug 30, 2012. 2 changed files with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions html_form
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    <form action="" enctype="multipart/form-data" method="post">
    <input id="file" name="file" type="file" />
    <input id="Submit" name="submit" type="submit" value="Submit" />
    </form>
    File renamed without changes.
  3. zvineyard created this gist Aug 30, 2012.
    47 changes: 47 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    <?php

    // Upload and Rename File

    if (isset($_POST['submit']))
    {
    $filename = $_FILES["file"]["name"];
    $file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
    $file_ext = substr($filename, strripos($filename, '.')); // get file name
    $filesize = $_FILES["file"]["size"];
    $allowed_file_types = array('.doc','.docx','.rtf','.pdf');

    if (in_array($file_ext,$allowed_file_types) &amp;&amp; ($filesize &lt; 200000))
    {
    // Rename file
    $newfilename = md5($file_basename) . $file_ext;
    if (file_exists("upload/" . $newfilename))
    {
    // file already exists error
    echo "You have already uloaded this file.";

    }
    else
    {
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename);
    echo "File uploaded successfully.";
    }
    }
    elseif (empty($file_basename))
    {
    // file selection error
    echo "Please select a file to upload.";
    }
    elseif ($filesize &gt; 200000)
    {
    // file size error
    echo "The file you are trying to upload is too large.";
    }
    else
    {
    // file type error
    echo "Only these file typs are allowed for upload: " . implode(', ',$allowed_file_types);
    unlink($_FILES["file"]["tmp_name"]);
    }
    }

    ?>