Hi,
this is my first post of what hopefully will be many. My knowledge of PHP + SQL is very basic at best so therefore I can imagine that this question really might have a simple solution.
What I'm trying to do is create a form that uploads a picture to my server, renames it and also adds texts to a database with the help of SQL.
When I uploaded a file I was always getting the error message saying (from upload2.php);
Files must be either JPEG, GIF, or PNG and less than 10,000 kb
I fiddled around and it dissappeared and started working fine. Now however it works great for me but when a friend of mine tries to upload a file it gives that same error message.
I think the problem lays in this part of the upload2.php part;
//checking if the uploaded file is of correct size or type
if (($_FILES["scanFile"]["type"] == "image/gif")
|| ($_FILES["scanFile"]["type"] == "image/jpeg")
|| ($_FILES["scanFile"]["type"] == "image/jpg")
|| ($_FILES["scanFile"]["type"] == "image/png")
&& ($_FILES["scanFile"]["size"] < 1000000)
)
But I can't for the life of me figure out what is wrong.
This is the code I've got in upload.php
<form enctype="multipart/form-data" action="upload2.php" method="post">
<input type="text" name="artist_a"><br />
<input type="text" name="side_a"><br />
<input type="text" name="artist_b"><br />
<input type="text" name="side_b"><br />
<input type="text" name="label"><br />
<textarea name="info" rows="4" cols="20"></textarea><br />
<input type="hidden" name="MAX_FILE_SIZE" value="1024000" />
<input type="file" name="scanFile" /><br />
<input type="submit" value="Upload!">
And this is the code that does all the work in upload2.php
<?php
$artist_a=$_POST['artist_a'];
$side_a=$_POST['side_a'];
$artist_b=$_POST['artist_b'];
$side_b=$_POST['side_b'];
$label=$_POST['label'];
$info=$_POST['info'];
// Connect to server
$con = mysql_connect("localhost", "****", "****")
or die("Connection failure");
// choose databas
mysql_select_db("****", $con);
//put the a variable to the filename of the uploaded picture
$fileName = $_FILES['scanFile']['name'];
$tmpName = $_FILES['scanFile']['tmp_name'];
//code for random name on uploaded files
//extracts the file extension and puts it in a variable
$ext = substr(strrchr($fileName, "."), 1);
//gives a random name
$randName = md5(rand() * time());
// catalogue where pictures are saves
$uploadDir = "labels/";
//adds everything together
$filePath = $uploadDir . $randName . '.' . $ext;
//checking if the uploaded file is of correct size or type
if (($_FILES["scanFile"]["type"] == "image/gif")
|| ($_FILES["scanFile"]["type"] == "image/jpeg")
|| ($_FILES["scanFile"]["type"] == "image/jpg")
|| ($_FILES["scanFile"]["type"] == "image/png")
&& ($_FILES["scanFile"]["size"] < 1000000)
)
{
//move the file
if(move_uploaded_file($tmpName, $filePath)){
//creates a variabel with the saved file path
$url = "http://www.reggaepedia.net/label_scan/labels/" . $randName . '.' . $ext;
//UPPDATES DB
//creates variable to make the SQL-query more easily understood
$filnamn = $randName . "." . $ext;
// SQL-query with the new post
$sql = "INSERT INTO label_scan(artist_a, side_a, artist_b, side_b, label, info, scan) VALUES('$artist_a', '$side_a', '$artist_b', '$side_b', '$label', '$info', '$url')";
//Run query
$result = mysql_query($sql, $con);
//Echo out the uploaded information
echo "<img src=\"" . $url . "\" alt=\"" . $randName . "\"><br/>";
echo "The file " . $randName . '.' . $ext . " has been uploaded <br/><br/>";
echo "<a href='http://www.reggaepedia.net/label_scan/'>Go back</a>";
}
//show errormessage if it couldn't be added.
else{
echo "There seems to be some kind of error. Echoing out the _FILES-array for controll:<br />";
echo "<pre>";
print_r($_FILES);
echo "</pre>";
}
}
else{ //Error if the file is of wrong size or type
echo "Files must be either JPEG, GIF, or PNG and less than 10,000 kb <br/><br/>";
}
// close connection
mysql_close($con);
?>