|
-
Jun 4th, 2009, 07:29 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Image upload problem !!!
i m trying to upload images and its working when i m running in my PC using wampserver but its not working from my isp server ... here is my full code ..
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>huntandblake.co.uk</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php
if(isset($_POST['Submit']))
{
$refer=$_POST['txtref'];
}
else
{
$refer=$_GET['ref'];
}
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","100");
//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['image']['name'];
//if it is not empty
if ($image)
{
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['image']['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['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo 'You have exceeded the size limit !';
$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="images/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'],$newname);
echo($_FILES['image']['tmp_name']);
echo("!and!");
echo("\n".$newname);
if (!$copied)
{
echo 'Copy unsuccessfull !';
$errors=1;
}}}}
//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors && $image)
{
echo "File Uploaded Successfully.";
mysql_connect ("","","");
mysql_select_db("huntandblake");
mysql_query("CREATE TABLE proimages(Reference varchar(12),ImageName varchar(16))");
mysql_query("INSERT INTO proimages VALUES('$refer','$image_name')");
}
?>
<!--next comes the form, you must set the enctype to "multipart/frm-data" and use an input type "file" -->
<form name="newad" method="post" action='uploadimage.php' enctype="multipart/form-data">
<table align='center' width='600'>
<tr><td><h3>Upload images</h3></td></tr>
<tr><td><input type="hidden" name="txtref" value="<?php echo($refer); ?>"></td></tr>
<tr><td><input type="file" name="image"></td></tr>
<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
</table>
</form>
</body>
</html>
Last edited by samsyl; Jun 5th, 2009 at 04:01 AM.
-
Jun 4th, 2009, 07:31 AM
#2
Thread Starter
Hyperactive Member
Re: Image upload problem !!!
Error message is: 'Copy unsuccessfull !'
-
Jun 4th, 2009, 07:45 AM
#3
Re: Image upload problem !!!
What permission do you have to the folder the images are being uploaded to? You may not have write access.
-
Jun 5th, 2009, 04:00 AM
#4
Thread Starter
Hyperactive Member
Re: Image upload problem !!!
it was realy silly .... yah now its working. thanks for your helpfull comment.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|