•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 427,379 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,008 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1052 | Replies: 2
![]() |
•
•
Join Date: May 2008
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
Hi! I'm trying to find a basic PHP code that uses an HTML form to upload an image to a specified directory and after that the image address can be called for displaying the image with PHP or putting it in a textarea box for linking. I know it seems simple but I've been looking everywhere for one. I must be looking in the wrong places. If anyone can give any help I would so appreciate it!
Thank you!
Thank you!
$image=$_FILES['broad1_image']['name'];
// if it is not empty
if ($image)
{
// get the original name of the file from the clients machine
$filename = stripslashes($_FILES['broad1_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, print an error message
//and will not upload the file, otherwise we continue
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
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=getimagesize($_FILES['broad1_image']['tmp_name']);
$sizekb=filesize($_FILES['broad1_image']['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($sizekb > 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=image1.'.'.$extension;
//the new name will be containing the full path where will be stored change these to the file where you would like to store the images. (images folder)
$broad1name="image/".$image_name;
$broad1name2="image/thumbs/thumb_".$image_name . $rand;
$copied = copy($_FILES['broad1_image']['tmp_name'], $broad1name);
$copied = copy($_FILES['broad1_image']['tmp_name'], $broad1name2);
//we verify if the image has been uploaded, and print error instead
if (!$copied) {
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}
else
{
// the new thumbnail image will be placed in images/thumbs/ folder
$thumb_name=$broad1name2;
// call the function that will create the thumbnail. The function will get as parameters
//the image name, the thumbnail name and the width and height desired for the thumbnail
$thumb=make_thumb($broad1name,$thumb_name,WIDTH,HEIGHT);
}
}
}
}
//If no errors registred, print the success message and show the thumbnail image created
if(isset($_POST['Submit']) && !$errors)
{
echo "<h5>Thumbnail created Successfully!</h5>";
echo '<img src="'.$thumb_name.'">';
}
?>
</p>
<form name="newad" method="post" enctype="multipart/form-data" action="">
<input type="file" name="broad1_image" >
<input name="Submit" type="submit" id="image1" value="Upload image" >
</form>that should do the job for you. i am using this on a website i am building
Last edited by kevin wood : May 15th, 2008 at 5:18 am.
•
•
Join Date: May 2008
Posts: 32
Reputation:
Rep Power: 1
Solved Threads: 0
if you are requiring a complete well documented and very much easy tutorial then follow the link www.php-mysql-tutorial.com. where everything about image upload is discussed in image gallery link..and plz let us confirm....whether it is enough???
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
access activation adsl1 adsl2 adsl2+ api blogger blogging blogs code code injection combo connection dani daniweb data development dreamweaver dropdownlist epilepsy gdata google gpl griefers hackers html image recognition image search internet kbps key linux microsoft module net news openbsd product programming reuse rss search serial tags video search vista web wysiwyg xml
- display image from databse (MySQL)
- is my code correct? (MySQL)
- file upload (JavaScript / DHTML / AJAX)
- upload and display images (PHP)
- upload and display images (PHP)
- Working with SQL server's Image data type (ASP.NET)
- Problems using a php generator (PHP)
- createimagefromjpeg(?) (PHP)
- how to display an attached image file in email (ASP)
Other Threads in the PHP Forum
- Previous Thread: Custom php.ini help
- Next Thread: need some help with a dynamically populated dropdown menu...


Linear Mode