0

I have a problem with uploading path in my sql database. Not displaying all path.

This is the php code:

$rd2 = mt_rand(1000, 9999) . "_File";
if ((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0))
    $filename = basename($_FILES['uploaded_file']['name']);

$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext != "exe") && ($_FILES["uploaded_file"]["type"] != "application/x-msdownload"));
    $newname = "uploads/" . $rd2 . "_" . $filename;

if (!file_exists($newname));

if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $newname)));
    $query = "insert into files (file_id,floc,subid,fname,fdesc,tcid)
              values ($newstd,'$newname',
                      '".htmlspecialchars($_REQUEST['subid'], ENT_QUOTES)."',
                      '".htmlspecialchars($_REQUEST['fname'], ENT_QUOTES)."',
                      '".htmlspecialchars($_REQUEST['fdesc'], ENT_QUOTES)."',
                      '".$_SESSION['tcid']."')";

if (!@executeQuery($query)) {
    if (mysql_errno () == 1062) //duplicate value
        $_GLOBALS['message'] = "Given Subject Name voilates some constraints,
                                please try with some other name.";
    else
        $_GLOBALS['message'] = mysql_error ();
}
else
    $_GLOBALS['message'] = "Successfully New Subject is Created.";
}

closedb();

The code is working and shows like this in database: https://i.stack.imgur.com/Z5jmb.png

It suppose to display uploads/2654_File_filename.docx

And the file is not uploaded.

The form is in a table:

<tr>
    <td> File</td>
    <td> 
        <form action="cursuri.php" method="post" enctype="multipart/form-data">
        <input type="file" name="uploaded_file" id="uploaded_file"></form>
    </td>
</tr>

I used this:http://www.w3schools.com/php/php_file_upload.asp but same no working

I'm using xampp 5.6.8 and php.ini and file_uploads directive is set to On.

EDIT - newstd is:

$result = executeQuery("select max(file_id) as fid from files");
    $r = mysql_fetch_array($result);
    if (is_null($r['fid']))
        $newstd = 1;
    else
        $newstd=$r['fid'] + 1;


    $result = executeQuery("select fname as fid from files where 
     fname='" . htmlspecialchars($_REQUEST['fname'], ENT_QUOTES) . "'
 and tcid=" . $_SESSION['tcid'] . ";");

    // $_GLOBALS['message']=$newstd;
    if (empty($_REQUEST['fname']) || empty($_REQUEST['fdesc'])) {
        $_GLOBALS['message'] = "Some of the required Fields are Empty";
    } else if (mysql_num_rows($result) > 0) {
        $_GLOBALS['message'] = "Sorry Subject Already Exists.";
    } else {

  }
  $rd2 = mt_rand(1000, 9999) . "_File";
...
...

EDIT: I forgot to mention that when i dont use a path he upload it in database.

Octavian
  • 43
  • 5
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 05 '15 at 14:29
  • 1
    @JayBlanchard their code is dying *Sam* – Funk Forty Niner Jun 05 '15 at 14:36
  • not to mention where `$newstd` is defined, or whether the session was even started. – Funk Forty Niner Jun 05 '15 at 14:41

2 Answers2

0

Can you check if the file is not too large and exceeds the limitations of these two configurations:

post_max_size
upload_max_filesize

You can see the values from a file with:

<?php
phpinfo();
?>

Edit:

Another suggestion is to put the body brackets of the if's because the single line execution if the statement is valid, cant be trusted. I think the problem can be the :

if (!file_exists($newname));

Also the way you insert into the DB you take the name from the $_REQUEST and not the new name and where do you store the path ?

mraiur
  • 44
  • 3
0

I had a "global" form and i putted enctype="multipart/form-data" in it and it worked with saving the path in table.

Before was in form below and dont recognise it.

<tr>
<td> File</td>
<td> 
    <form action="cursuri.php" method="post" enctype="multipart/form-data">
    <input type="file" name="uploaded_file" id="uploaded_file"></form>
</td>

But still no uploading files...

Octavian
  • 43
  • 5