PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
$uploaddir='/home/vagonweb/public_html/donotask/pics/';
if ($_FILES['thepic']['type'] != "image/pjpeg" ) {
echo "the image must be a jpeg!";
}
else {
if (move_uploaded_file($_FILES['thepic']['tmp_name'], $uploaddir . $_FILES['thepic']['name']))
{
$sql="INSERT INTO pics (submitter, title, description, category, url) VALUES ($_SESSION['username'], '$title', '$description', '$category', $_FILES['thepic']['name'])";
$result=mysql_query($sql,$db);
echo "The file was successfully uploaded!";
}
else
{
echo "Your file could not be copied.";
}
unlink($thepic);
The $sql line seems to be the culprit, but I can't figure out what's wrong. Quote mishap perhaps?
evilmonkey wrote:Thank you for the reply, EDIT: I understan now. The code you gave me checks for MySQl errors. This spits out a PHP error before mysql is even reached.
Thanks!
np. hehe. i nearly made the mistake of saying after that instead of after result because i do it a bit different, so i understand why you made that mistake., it's very logical
nvmd. keep that incase you have a sql error. it'll help. i see the error.
you're using an arra element directly in the string. you have to put {} in a certain place or it will give errors. you can break it up ... try making......
$uploaddir='/home/vagonweb/public_html/donotask/pics/';
if ($_FILES['thepic']['type'] != "image/pjpeg") {
echo $_FILES['thepic']['type'];
echo "the image must be a jpeg!";
}
else {
if (move_uploaded_file($_FILES['thepic']['tmp_name'], $uploaddir . $_FILES['thepic']['name']))
{
$submitter=$_SESSION['username'];
$url=$_FILES['thepic']['name'];
$sql="INSERT INTO pics (submitter, title, description, category, url) VALUES ('$submitter', '$title', '$description', '$category', '$url')";
$errno=mysql_errno($db);
$error=mysql_error($db);
$debug="query: $sql<br />error number: $errno<br />error: $error";
echo $debug;
$result=mysql_query($sql,$db);
echo "The file was successfully uploaded!";
The problem is, it always tells me that the image must be a jpeg when it really is. When i ask to echo the $_FILE['thepic']['type'], it comes out blank. What is the problem?
$uploaddir='/home/vagonweb/public_html/donotask/pics/';
if ($_FILES['thepic']['type'] != "image/pjpeg") {
echo $_FILES['thepic']['type'];
echo "the image must be a jpeg!";
}
else {
if (move_uploaded_file($_FILES['thepic']['tmp_name'], $uploaddir . $_FILES['thepic']['name']))
{
$submitter=$_SESSION['username'];
$url=$_FILES['thepic']['name'];
$sql="INSERT INTO pics (submitter, title, description, category, url) VALUES ('$submitter', '$title', '$description', '$category', '$url')";
$errno=mysql_errno($db);
$error=mysql_error($db);
$debug="query: $sql<br />error number: $errno<br />error: $error";
echo $debug;
$result=mysql_query($sql,$db);
echo "The file was successfully uploaded!";
The problem is, it always tells me that the image must be a jpeg when it really is. When i ask to echo the $_FILE['thepic']['type'], it comes out blank. What is the problem?
Thanks for your help.
i had that too. you see it's 1: guessed at by the user's browser
2: optional to do by the browser
to actually get it use getImageSize
it returns an array, so you'd so something like
now you can check height and width if you want. you see....
$imageInfo[0]==width in pixels
$imageInfo[1]==height in pixels
$imageInfo[2]==image type (check link for the numbers and equivalents. jpeg is 2)
$imageInfo[3]==string that can be placed into a img tag that gives the image's height and width
// amend form_field_name to suit the name you used.
$err_code = $_FILES['form_field_name']['error'];
$uplo_return_str = '';
if($err_code != 0)
{
// amend the [89,000] to suit the MAX_FILE_SIZE as set in the html
$uplo_return_str = ($err_code < 5) ? ($err_code < 4) ? ($err_code < 3) ? ($err_code < 2) ?
'Upload File was above ' .ini_get('upload_max_filesize'). ' bytes in size.<br />' :
'Upload File was above [ 89,000 ] bytes in size.<br />' :
'Upload File was only partially uploaded, please try again.<br />' :
'No Upload File received, please try again.<br />' :
'Unknown Error Encountered, please try uploading again.<br />' ;
}
else
{
// successful upload of something - do more tests - add rest of upload code
// once again amend form_field_name to suit the name you used.
$img_size = getimagesize($_FILES['form_field_name']['tmp_name']);
if($img_size[2] == 2)
{
// woot - a jpeg successfully uploaded
// we also now have $img_size[0] holding the width and $img_size[1] the height
move_uploaded_file($_FILES['form_field_name']['tmp_name'], 'somewhere/somename.jpg');
$uplo_return_str = 'w00t w00t - methinks that was successful.<br />';
}
else
{
$uplo_return_str = 'Now, that was a jpeg was it? - I think it wasn''t - wanna try again?<br />';
}
}
echo $uplo_return_str;
returns a bit more information - might be helpful for debugging. Also use getimagesize to pull the mime type rather than phps $_FILES[]['type'] method.
if ($_SESSION['auth']===true)
{
if ($foo=="bar")
{
$err_code = $_FILES['thepic']['error'];
$uplo_return_str = '';
if($err_code != 0)
{
// amend the [89,000] to suit the MAX_FILE_SIZE as set in the html
$uplo_return_str = ($err_code < 5) ? ($err_code < 4) ? ($err_code < 3) ? ($err_code < 2) ?
$msg='Upload File was above ' .ini_get('upload_max_filesize'). ' bytes in size.<br />' :
$msg='Upload File was above [ 89,000 ] bytes in size.<br />' :
$msg='Upload File was only partially uploaded, please try again.<br />' :
$msg='No Upload File received, please try again.<br />' :
$msg='Unknown Error Encountered, please try uploading again.<br />' ;
echo $msg;
}
else
{
// successful upload of something - do more tests - add rest of upload code
// once again amend form_field_name to suit the name you used.
$img_size = getimagesize($_FILES['thepic']['tmp_name']);
if($img_size[2] == 2)
{
// woot - a jpeg successfully uploaded
// we also now have $img_size[0] holding the width and $img_size[1] the height
if (move_uploaded_file($_FILES['thepic']['tmp_name'], 'pics/'.$_FILES['thepic']['name']))
{
$submitter=$_SESSION['username'];
$url=$_FILES['thepic']['name'];
$sql="INSERT INTO pics (submitter, title, description, category, url) VALUES ('$submitter', '$title', '$description', '$category', '$url')";
$errno=mysql_errno($db);
$error=mysql_error($db);
$debug="query: $sql<br />error number: $errno<br />error: $error";
echo $debug;
$result=mysql_query($sql,$db);
echo "The file was successfully uploaded!";
}
else
{
echo "Your file could not be copied.";
}
unlink($thepic);
}
}
}
else
{
echo "<p align="center">";
echo "<form method="post" enctype="multipart/form-data" action="$PHP_SELF">";
echo "<input type="hidden" name="MAX_FILE_SIZE" value="10000000000">";
echo "Title:<br>";
echo "<input type="text" name="title" size="50"><br>";
echo "Description:<br>";
echo "<textarea name="description" cols="40" rows="7"></textarea><br>";
echo "File:<br>";
echo "<input type="file" name="thepic"><br>";
echo "Category:<br>";
echo "<select name="category">";
echo "<option value="pixel">Pixel</option>";
echo "<option value="photo">Photo</option>";
echo "<option value="photo_manip">Photo Manipulation </option>";
echo "<option value="sketch">Sketch</option>";
echo "<option value="digital">DigiArt</option>";
echo "<option value="3d">3D</option>";
echo "<option value="classic">Classic</option>";
echo "<option value="other">Other</option>";
echo "</select><br>";
echo "<input type="submit">";
echo "<input type="hidden" name="foo" value="bar">";
echo "</form>";
echo "</p>";
}
}
else
{
echo "Unathorized access";
}
?>
Now it tells me that the upload is too big. Notice the MAX_FILE_SIZE in the form, I theoretically shouldn't be having problems with a 100 kb jpg image...