0

Hi so I am trying to upload an image to my db and a folder as well as other elements from my form, is all working great except I can't get the file to appear in the uploads folder. Here is my code:

if(isset($_POST['submit'])){
    //This gets all the other information from the form
    $name = $_POST['name'];
    $description = $_POST['description'];
    $founded = $_POST['founded'];
    $category = $_POST['category'];
    $fileName = $_FILES['userfile']['name'];
    $tmpName  = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];

    $uploaded_dir = "/httpdocs/uploads/";
    $path = $uploaded_dir . $fileName;

    print "Temporary name: " . $_FILES['userfile']['tmp_name'] . "<br>";
    print "Original name: $filename<br>";
    print "Destination: $path<br>";

    if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $path)) {
        print "Uploaded file moved";
        // do something with the file here
    } else {
        print "Move failed";
    }

    $fp      = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpName));
    $content = addslashes($content);
    fclose($fp);

    if(!get_magic_quotes_gpc()){
        $fileName = addslashes($fileName);
    }

    // Connects to your Database
    mysql_connect("localhost", "root", "") or die(mysql_error()) ;
    mysql_select_db("my_db") or die(mysql_error()) ;


    //Writes the information to the database
    $query = "INSERT INTO mytable (name, description, founded, category, logo)".
    "VALUES ('$name', '$description', '$founded', '$category', '$fileName')";

    mysql_query($query) or die('Error, query failed');
    include 'library/closedb.php';

    echo "<br>File $fileName uploaded<br>";
}

This is the response I get every time

Temporary name: /tmp/phpA8JjRz
Original name:
Destination: /httpdocs/uploads/fixed.png
Move failed
File fixed.png uploaded

This is my form

<form method="POST" action="path" enctype="multipart/form-data">
<label>Flying School</label><br />
<input type="text" name="name" id="fsn" placeholder="Flying School Name" required/><br />
<label>Category</label><br />
<select name="category">
  <option value="0">one</option>
  <option value="1">two</option>
  <option value="2">three</option>
  <option value="3">four</option>
</select><br />
<label>Founded</label><br />
<input type="text" name="founded" id="founded" placeholder="yyyy-mm-dd" /><br />
<label>Logo</label><br />
<!--<div class="uploadlogo">-->
<input type="file" name="userfile" />
<!--</div>--><br />
<label>Cover Image</label><br />
<div class="coverimage">
<input type="file" name="cover" id="cover" />
</div><br />
<label>Description</label><br />
<?php
  if (isset($_POST['description'])) $initialentry=$_POST['description'];
  else $initialentry='';
  $editor = JFactory::getEditor();
  echo $editor->display( 'description',  $initialentry, '80%', '350', '55', '20', false  ) ;
?><br />
<label>Photos</label><br />
<ul id="addPhotos">
<li><div class="upload1">
<input type="file" name="acimg1" id="acimg1" />
</div></li>
<li><div class="upload2">
<input type="file" name="acimg2" id="acimg2" />
</div></li>
<li><div class="upload3">
<input type="file" name="acimg3" id="acimg3" />
</div></li>
</ul>
<br />
<label>Choose here</label><br />
<label class="checkbox-inline">
  <input type="checkbox" value="Rss">R22
</label>
<label class="checkbox-inline">
  <input type="checkbox" value="R44">R44
</label>
<label class="checkbox-inline">
  <input type="checkbox" value="R66">R66
</label>
<label class="checkbox-inline">
  <input type="checkbox" value="AS355">AS355
</label>
<label class="checkbox-inline">
  <input type="checkbox" value="PA28">PA28
</label>
<label class="checkbox-inline">
  <input type="checkbox" value="CESSNA172">Cessna 172
</label>
<label class="checkbox-inline">
  <input type="checkbox" value="CESSNA152">Cessna 152
</label><br />
<input class="btn btn-primary" type="submit" name="submit" value="Submit" />

thanks in advance!

Dave Lynch
  • 65
  • 7
  • check folder permissions and the path. error reporting will help you here – Funk Forty Niner Jun 28 '16 at 23:59
  • Plus, we've no way to tell if your form is correct or not – Funk Forty Niner Jun 29 '16 at 00:01
  • @Fred-ii- ive added the form – Dave Lynch Jun 29 '16 at 00:07
  • Thanks Dave. It looks ok then. My first comment mentioned to check the folder's permissions if it can be written to, and if the path is correct. Use error reporting also. – Funk Forty Niner Jun 29 '16 at 00:08
  • 1
    `/httpdocs/uploads/` that should be a full system path. I.e.: `/var/usr/httpdocs/uploads/` or a relative path I.e.: `../uploads/`. I doubt your root is `/httpdocs/`. Use `phpinfo()` to check what the root is. – Funk Forty Niner Jun 29 '16 at 00:10
  • the folder is set to 755 and error reporting not sure – Dave Lynch Jun 29 '16 at 00:10
  • I wrote another comment above yours ^. Let me know about that. I'll delete this comment shortly in order not to tie up the comments section too much. Ping me, I have to leave for 1/2 hour. – Funk Forty Niner Jun 29 '16 at 00:15
  • SOLVED!!! thanks man I got the true path for the folder and its worked like a dream!! you my friend are a life saver! – Dave Lynch Jun 29 '16 at 00:17
  • Some time ago I faced a problem when I was testing a file upload form in a web application I had worked on. Everything was going fine, the record of the file was being correctly written to the database table it should have written to, but the file was not being upload: move_uploaded_file() seemed not to be working if the file was coming from the same server (a.k.a.: it was not an uploaded file). Not sure about your scenario, so I post this as a comment, it might be useful ;) – PoPeio Jun 29 '16 at 00:19
  • 2
    @Velnias this is the exact issue i had but thanks to Fred I changed the path to my full path and it worked like a dream – Dave Lynch Jun 29 '16 at 00:22
  • 1
    @Fred-ii- sounds like an answer to me. – chris85 Jun 29 '16 at 00:36
  • This code is open to SQL injections. – chris85 Jun 29 '16 at 00:46
  • @DaveLynch Hey Dave, I'm back. I'm glad to hear that it worked out and I've put my comment to an answer below, *cheers* – Funk Forty Niner Jun 29 '16 at 02:06
  • @chris85 I have Chris. I was away for a bit and came back to post my answer below, *cheers*. – Funk Forty Niner Jun 29 '16 at 02:13
  • @DaveLynch *"SOLVED!!! thanks man I got the true path for the folder and its worked like a dream!! you my friend are a life saver!"* - You're most welcome Dave. – Funk Forty Niner Jun 29 '16 at 02:17
  • Sounds like it could be a permissions issue. Check ownership of your script and make sure it's not owned by root, choown it to apache:apache. Also make sure the folder you're writing the file to is writable - 755 permission level. – Gary Mathis Jun 29 '16 at 02:19
  • @DaveLynch you realize the question is still considered as open/unsolved. I posted my answer on Jun 29 – Funk Forty Niner Aug 05 '16 at 11:44

2 Answers2

1

"SOLVED!!! thanks man I got the true path for the folder and its worked like a dream!! you my friend are a life saver! – Dave Lynch"

Putting my comment to an answer:

/httpdocs/uploads/ that should be a full system path.

I.e.: /var/usr/httpdocs/uploads/ or a relative path. I.e.: ../uploads/.

I doubt your root is /httpdocs/. System paths usually start by /var/usr/ on most servers. Others have something like /var/usr/public_html/ etc. There are more, but these are enough examples.

Using phpinfo() will show you what your system path is.

Also as stated in comments:

Your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

Firstly: Check the administrator permission for the path you have selected to move your uploaded file

Secondly: Check the root folder if it exist in directory path/ else you would not be able to move uploaded file. try ../dirname to back and change your dir

Syed Aqeel
  • 1,009
  • 2
  • 13
  • 36