Page 1 of 1

Uploading files to server

Posted: Tue Nov 17, 2009 1:51 pm
by flx666
Hi guys.
I am pretty new in PHP, but I am trying as good as I can. :-)

I have some code where I try to upload 2 files to my server, and write the filname, description etc. to mySql database
But no files is uploaded and it just write:
"Stored in: upload/"

Could somebody please help me? I am pretty lost here.

Here is my code:

<html>
<body>

<form action="uploadp.php" method="post" enctype="multipart/form-data">

<p>Upload filer:<br>
<input type="file" name="minfil[]"><br>
<input type="file" name="minfil[]"><br>


Overskrift:<input type="text" name="overskrift"/><br><br>

Beskrivelse:<input type="text" name="beskrivelse"/><br><br>

</p>

<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>



<?php
$host = "x";
$user = "x";
$pass = "x";
$db = "x";
$connection = mysql_connect($host,$user,$pass);
mysql_select_db("$db");

$uploads_dir = 'upload/';

$noFiles = count($_FILES["file"]["name"]); for($i = 0; $i < $noFiles; $i++) {

echo "Upload: " . $_FILES["file"]["name"][$i] . "<br />";
echo "Type: " . $_FILES["file"]["type"][$i] . "<br />";
echo "Size: " . ($_FILES["file"]["size"][$i] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"][$i] . "<br />";

if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
$picName = $_FILES['file']['name'];
}

}

$sql = "INSERT INTO show_nyheder (filnavn, dato, overskrift, beskrivelse, kategori) VALUES('$picName', NOW(), '$_POST[overskrift]', '$_POST[beskrivelse]', '$_POST[kategori]');";
mysql_query($sql); // Sender sql sætnignen til MySQL databasen
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

Re: Uploading files to server

Posted: Tue Nov 17, 2009 1:53 pm
by iankent
at a quick glance (in a bit of a rush sorry), but you've named your file input fields minfil and you're trying to read them in via PHP as called 'file'

try changing $_FILES["file"] to $_FILES["minfil"]

hth

Re: Uploading files to server

Posted: Tue Nov 17, 2009 2:08 pm
by flx666
Haha, stupid me.. And I have been looking at anything else but the name... :-)
Thanks a lot... It helped... :-)

But still have problems, it tries to upload but it says that the file exists - but it doesnt???

Upload: hyggesep (21).jpg
Type: image/pjpeg
Size: 146.010742188 Kb
Temp file: /var/www/clients/client1/web1/tmp/phpulMOEy
already exists. Upload: hyggesep (28).jpg
Type: image/pjpeg
Size: 29.9423828125 Kb
Temp file: /var/www/clients/client1/web1/tmp/phptPZ8Pz
already exists. Stored in: upload/

Re: Uploading files to server

Posted: Tue Nov 17, 2009 2:29 pm
by iankent
edit: rearranged post so it makes sense :P

just noticed - on this line:
if (file_exists("upload/" . $_FILES["file"]["name"]))
you're referring to it as $_FILES['file']['name'] and it should be $_FILES['file']['name'][$i] (notice the missing $)

if that doesn't work:
Well its clearly finding the correct info so the next thing I'd check is $_FILES['minfil']['error'] to see whether there were any problems during the upload.

Also, have you set the correct permissions on the upload folder? You'll need to chmod the folder to at least 666, though most people would use 766 or 777.

hth :)

Re: Uploading files to server

Posted: Tue Nov 17, 2009 2:33 pm
by flx666
Found the error... Missed a $ in :-)

Re: Uploading files to server

Posted: Tue Nov 17, 2009 2:35 pm
by flx666
Ahhh.. Didn't see your answer before now.. But then I learned something.... :-)

But thanks a lot for your fast answer and help.. Very appreciated.... :drunk: :P