How to retrieve uploaded files using php

前端 未结 4 1667
无人共我
无人共我 2021-01-16 04:49

Ok. I have searching on this site Since August 24 2011.(not that it matters) for a way to display files that have been uploaded by a user. I have my form on the admin side a

相关标签:
4条回答
  • 2021-01-16 05:30

    Something very basic that you might have missed is the form enctype. Many begginers does this. So just check if you have the enctype attribute in the form.

    <form enctype="multipart/form-data">
    

    Only if you have this you can upload files through your forms. Another place where you could possibly gone wrong is

    $query = "INSERT INTO user_DB VALUES ('','$company', '$location', '$userfile' )";
    

    You store the file name in a variable called $pic but here you have given $userfile so just try changing that into $pic.

    0 讨论(0)
  • 2021-01-16 05:35

    Some suggestions for what you could change to get this working.

    1. Upload form

    What does your form tag look like? Don't forget to include the enctype parameter as per below:

    <form type="post" action="" enctype="multipart/form-data">
        ...
    </form>
    

    2. Sanitisation

    $company  = mysql_real_escape_string($_POST['company']); 
    $location = mysql_real_escape_string($_POST['location']);
    $pic      = mysql_real_escape_string($_FILES['userfile']['name']);
    

    The above lines are the first step in helping to prevent your queries from suffering SQL injection attacks.

    3. SQL Query

    $userfile does not exist as you have actually assigned the file name to $pic instead so your query should look like this:

    $query = "INSERT INTO user_DB 
              VALUES ('','$company', '$location', '$pic')";
    

    4. HTML Output

    Now to link to the file in your output table:

    echo "<td>";
    echo "<a href=" . $target_path . basename($row['userfile']) . ">
             {$row['userfile']}</a>";
    echo "</td>";
    
    0 讨论(0)
  • 2021-01-16 05:43

    i think sould replace this

    $query = "INSERT INTO user_DB VALUES ('','$company', '$location', '$userfile' )";
    

    with

    $query = "INSERT INTO user_DB VALUES ('','$company', '$location', '$pic' )";
    
    0 讨论(0)
  • 2021-01-16 05:47

    you can use one column as auto_increament

    when you are inserting a new user than you can get sum of already registered users using this query

    $qry = mysql_qyery("select * from users"); and you can get user count by using

    $count = mysql_num_rows($qry); so after this you insert a new user and his ID will be

    $count1 = $count+1; $id = 'F'.$count1;

    0 讨论(0)
提交回复
热议问题