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!
<?php
if ($_FILES['image']['error'] !== 0)
{
Header('Location: index.php?e=' . $_FILES['image']['error']);
}
$file_tmp_name = $_FILES['image']['tmp_name'];
// Get the dimensions of the image
$size = getimagesize($file_tmp_name);
$image_width = $size[0];
$image_height = $size[1];
$image_mime = $size['mime'];
if ($image_mime == '')
{
Header('Location: index.php?e=5');
}
// Swap the slashes around on a Windows server otherwise MySQL freaks
$file_tmp_name = str_replace('\'', '/', $file_tmp_name);
// Save it to MySQL - normally I'd use a DB abstraction class, but here we need results fast
dbi();
$sql = "
INSERT INTO
porn
(
data,
width,
height,
mime
) VALUES (
LOAD_FILE('$file_tmp_name'),
'$image_width',
'$image_height',
'$image_mime'
)
";
mysql_query($sql);
if (mysql_error())
{
$error = mysql_error();
}
else
{
$error = false;
$id = mysql_insert_id();
}
mysql_close();
?>
this uploaded images to a database but it only allows gif images how can i change it to make it into jpg as well
there's either a hidden field in the form posting the image or it's pulling the setting out of your php.ini file. Or rather.. there isn't such a hidden field, which it expects.. and so defaults to 0.
<?php
<?php
include("mainfile.php");
if (isset($_GET['e']))
{
switch ($_GET['e'])
{
case 1:
$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
break;
case 2:
$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
break;
case 3:
$error = 'The uploaded file was only partially uploaded.';
break;
case 4:
$error = 'No file was uploaded.';
break;
case 5:
$error = 'The image MIME type could not be determined - are you sure you uploaded a valid image file?';
break;
default:
$error = 'An un-specified error occured';
}
}
else
{
$error = false;
}
?>
<html>
<head>
<title>How to store an image into a MySQL BLOB field</title>
<style>
BODY, TD {
font-family: Arial, Helvetica;
font-size: 12px;
color: black
}
</style>
</head>
<body>
<h1>Images <> MySQL via PHP</h1>
<p>Your web server is configured as follows:</p>
<table border=1>
<tr><td>File Uploads Enabled:</td><td><?=ini_get('file_uploads')?></td></tr>
<tr><td>Upload Max Filesize</td><td><?=ini_get('upload_max_filesize')?></td></tr>
<tr><td>Upload Temp Directory</td><td><?=ini_get('upload_tmp_dir')?></td></tr>
<tr><td>POST Max Size</td><td><?=ini_get('post_max_size')?></td></tr>
</table>
<?php
if ($error)
{
echo "<p><font color=red><b>ERROR: $error</b></font></p>";
}
?>
<p>
<form action="upload_image.php" method=post enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="<?=ini_get('upload_max_filesize')?>">
Image File: <input type=file name="image" accept="image/jpeg,image/gif,image/x-png"><br>
<input type=submit value="Upload">
</form>
</p>
</body>
</html>
?>
Ok there is the hidden input but it is set to 2 megs. and i am trying to upload 30 k
<?php
<?php
include("mainfile.php");
if (isset($_GET['e']))
{
switch ($_GET['e'])
{
case 1:
$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
break;
case 2:
$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
break;
case 3:
$error = 'The uploaded file was only partially uploaded.';
break;
case 4:
$error = 'No file was uploaded.';
break;
case 5:
$error = 'The image MIME type could not be determined - are you sure you uploaded a valid image file?';
break;
default:
$error = 'An un-specified error occured';
}
}
else
{
$error = false;
}
?>
<html>
<head>
<title>IMG</title>
<style>
BODY, TD {
font-family: Arial, Helvetica;
font-size: 12px;
color: black
}
</style>
</head>
<body>
<?php
if ($error)
{
echo "<p><font color=red><b>ERROR: $error</b></font></p>";
}
?>
<p>
<form action="upload_image.php" method=post enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="<?=ini_get('upload_max_filesize')?>">
Image File: <input type=file name="image" accept="image/jpeg,image/gif,image/x-png"><br>
<input type=submit value="Upload">
</form>
</p>
</body>
</html>
?>