Hi again, I am having trouble with an image uploader, it connects to the mysql database i think but doesn't input the data, the form is on a different page from the database upload, and then it outputs onto another page altogether.
The image is connected to a directory in the server, but im thinking that it might not be working because i am using MySQL database on 000webhost.com
the code is as follows.
Form:
Code:
<form name="webUpload" id="webUpload" enctype="multipart/form-data" method="post" action="http://www.infordesign.webege.com/webUpload.php">
<table>
<tr>
<td class="labelCell"><label for="webName">Name of Website</label></td>
<td class="fieldCell"> <input type="text" name="webName" id="webName" tabindex="1" /></td>
</tr>
<tr>
<td class="labelCell"><label for="webImage">Web Image</label></td>
<td class="fieldCell"> <input type="file" name="webImage" id="webImage" tabindex="2" /></td>
</tr>
<tr>
<td class="labelCell"><label for="webWeb">Website Address</label></td>
<td class="fieldCell"> <input type="text" name="webWeb" id="webWeb" tabindex="2" /></td>
</tr>
<tr>
<td colspan="2" class="fieldCellButton"><input type="submit" name="submit" value="Submit Web Design" tabindex="3" /></td>
</tr>
</table>
</form>
Database upload:
Code:
<?php
header ('Location: http://www.infordesign.webege.com/webUploadForm.php');
?>
<?php
$con = mysql_connect("hostname","a3127580_Tables","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("table", $con);
$sql="INSERT INTO webDesign (webName, webImage, webWeb)
VALUES('$_POST[webName]','$_POST[webImage]','$_POST[webWeb]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "You have uploaded a web design successfully";
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","1000");
//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
//This variable is used as a flag. The value is initialized with 0 (meaning no error found)
//and it will be changed to 1 if an errro occures.
//If the error occures the file will not be uploaded.
$errors=0;
//checks if the form has been submitted
if(isset($_POST['submit']))
{
//reads the name of the file the user submitted for uploading
$image=$_FILES['webImage']['name'];
//if it is not empty
if ($image)
{
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['webImage']['name']);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file,
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
echo '<h1>Unknown extension!</h1>';
$errors=1;
}
else
{
//get the size of the image in bytes
//$_FILES['webImage']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['webImage']['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="http://file-manager.000webhost.com/index.php/public_html/images/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['webImage']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}
//If no errors registred, print the success message
if(isset($_POST['submit']) && !$errors)
{
echo "<h1>File Uploaded Successfully! Try again!</h1>";
}
mysql_close($con)
?>
Output:
Code:
<?php
$con = mysql_connect("host","a3127580_Tables","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("table", $con);
$result = mysql_query("SELECT * FROM webDesign ORDER BY ID");
while($row = mysql_fetch_array($result))
{
echo "<table style='border: 1px solid #999; width: 200px; background-color: #f9fbfd; color: #222;'><tr><td style='border: 1px solid #999; color: #222; font-size: 12px;' align='center'>".$row['webName']."</td></tr><tr><td><a href = 'http://" . $row['webWeb'] . "' target='_blank'><img src='" . $row['webImage'] . "' style='width: 200px; height: 180px; border: 0px;' /></a></td></tr>";
}
echo "</table>";
mysql_close($con);
?>
Last edited by lynxus; 12-10-2010 at 11:28 AM..
Reason: Removed some private Mysql login information
|