File Upload

Discussion in 'PHP' started by masterofzoo, Sep 22, 2011.

  1. #1
    I am working on my website and this website will require users to upload files;
    I have tried many ways to make this file uploaded to a folder and its location in database,
    I even cannot make the file stored in a folder in local host
    actually I am using "Dreamweaver 8"; this is the code I have used ...............please help:-


    ////////////the code
    <?php
    if($_POST['submit']){
    include ("Ile Uploading.php");
    $name = $_FILES['upload']['name'];
    $temp = $_FILES['upload']['temp_name'];
    $type= $_FILES['upload']['type'];
    $size = $_FILES['upload']['size'];
    if(($type=="image/jpeg") or ($type=="image/jpg") or ($type=="image/gif")){
    if ($size<=100000){
    move_uploaded_file($temp,$name);
    echo "<img src = '$name'>";
    }else{
    echo"The file:$name is too large...<br>
    The size is $size and needs to be less tan 100MB";
    }
    }else{
    echo "This type $type is not allowed";
    }
    }else{
    header("location: Ile Uploading.php");
    }
    ?>
    //////////////end
     
    masterofzoo, Sep 22, 2011 IP
  2. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #2
    you must put the full directory path (not url path) of the file destination to do it..

    move_uploaded_file($temp,'c:/path/to/your/uploads/folder/'.$name);
    PHP:
     
    JohnnySchultz, Sep 23, 2011 IP
  3. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #3
    There seems you did not specify any folder it should upload to.

    $dir = "./upload_dir"
    move_uploaded_file($temp,"{$dir}/{$name}");
    PHP:
    Replace 'upload_dir' with your sub-folder name.
    Try and let me know results.

    regards
     
    Vooler, Sep 23, 2011 IP
  4. masterofzoo

    masterofzoo Peon

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    None of them works......I tried that:-
    ////////////the code
    <?php
    if($_POST['submit']){
    include ("Ile Uploading.php");
    $name = $_FILES['upload']['name'];
    $temp = $_FILES['upload']['temp_name'];
    $type= $_FILES['upload']['type'];
    $size = $_FILES['upload']['size'];
    if(($type=="image/jpeg") or ($type=="image/jpg") or ($type=="image/gif")){
    if ($size<=100000){
    ove _ uploaded _ file($temp,'c:/path/to/your/uploads/folder/'.$name);(does not work)
    echo "<img src = '$name'>";
    }else{
    echo"The file:$name is too large...<br>
    The size is $size and needs to be less tan 100MB";
    }
    }else{
    echo "This type $type is not allowed";
    }
    }else{
    header("location: Ile Uploading.php");
    }
    ?>
    //////////////end
    as well as this :-
    ////////////the code
    <?php
    if($_POST['submit']){
    include ("Ile Uploading.php");
    $name = $_FILES['upload']['name'];
    $temp = $_FILES['upload']['temp_name'];
    $type= $_FILES['upload']['type'];
    $size = $_FILES['upload']['size'];
    if(($type=="image/jpeg") or ($type=="image/jpg") or ($type=="image/gif")){
    if ($size<=100000){
    $dir = "./upload_dir"
    ove_uploaded_file($temp,"{$dir}/{$name}");(there is an error here)
    echo "<img src = '$name'>";
    }else{
    echo"The file:$name is too large...<br>
    The size is $size and needs to be less tan 100MB";
    }
    }else{
    echo "This type $type is not allowed";
    }
    }else{
    header("location: Ile Uploading.php");
    }
    ?>
    //////////////end
     
    masterofzoo, Sep 24, 2011 IP
  5. workingsmart

    workingsmart Well-Known Member

    Messages:
    411
    Likes Received:
    12
    Best Answers:
    9
    Trophy Points:
    120
    #5
    Is the folder you are trying to upload have the proper permissions?
     
    workingsmart, Sep 24, 2011 IP
  6. masterofzoo

    masterofzoo Peon

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The folder is "READ ONLY" however, I tried to change the properties and it returns to "Read Only", should I try it through PHP code?
     
    masterofzoo, Sep 24, 2011 IP
  7. masterofzoo

    masterofzoo Peon

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I have tried another codes from "W3school", and I don't know what I have to do:- "what is the mistakes?".
    The code is :-
    ///////////////////Beginning(this is the action php file of the form file)
    <?php
    if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
    else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 20000))
    {
    if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
    else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
    }
    else
    {
    echo "Invalid file Properties";
    }
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 20000))
    {
    if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
    else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";


    if (file_exists("C:\AppServ\www\Mysite\Images\Images/" . $_FILES["file"]["name"]))
    {
    echo $_FILES["file"]["name"] . " already exists. ";
    }
    else
    {
    move_uploaded_file($_FILES["file"]["tmp_name"],
    "C:\AppServ\www\Mysite\Images\Images/" . $_FILES["file"]["name"]);
    echo "Stored in: " . "C:\AppServ\www\Mysite\Images\Images/" . $_FILES["file"]["name"];
    }
    }
    }
    else
    {
    echo "Invalid file";
    }
    ?>
    ///////////last
     
    masterofzoo, Sep 24, 2011 IP
  8. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #8
    I'm not reacting on your problem, your answer is found easily but i'm just want to point you to security problems.

    NEVER and i'll say again NEVER trust the 'filename' given by the user. Always use your OWN created filename and extension. Your example is very easy to abuse and hack your server!!!!

    b.t.w. move_file_uploaded doesn't need to specify a directory if you are using 'current' directory! :)

    Good luck and please check my comment and fix your security issue
     
    EricBruggema, Sep 24, 2011 IP