[SOLVED] Image Upload with PHP/MySQL

This is a discussion on "[SOLVED] Image Upload with PHP/MySQL" within the PHP Forum section. This forum, and the thread "[SOLVED] Image Upload with PHP/MySQL are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Jan 25th, 2008, 17:10
Junior Member
Join Date: Mar 2007
Location: indiana
Age: 25
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Image Upload with PHP/MySQL

I've read through other forums on this topic, and I'm having so much trouble I could kick a cat.

Can anyone recommend a good tutorial or walk-through to upload image info to a database and the image itself into a folder on the server, and also be able to display the images to the web browser.

All the tutorials I've found are outdated, or aren't working.

Any help would be greatly appreciated.

Thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jan 26th, 2008, 02:50
Most Reputable Member
Join Date: Feb 2004
Location: Borneo
Age: 27
Posts: 1,612
Blog Entries: 2
Thanks: 0
Thanked 4 Times in 3 Posts
Re: Image Upload with PHP/MySQL

You can go through with all the great PHP tutorial from W3Schools.
I might think that this will help you!

[PHP File Upload]
Last Blog Entry: ASP Programming Tips and Technique (Oct 26th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jan 26th, 2008, 06:43
Reputable Member
Join Date: Nov 2007
Location: India
Posts: 150
Blog Entries: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Image Upload with PHP/MySQL

I don't know where I had obtained this code from www.tizag.com:

HTML form for user:
HTML: Select all
<html>
<head>
<title>Upload a file</title>
</head>
<body>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
PHP script on server:
PHP: Select all

<?
// Where the file is going to be placed 
$target_path "uploads/";

/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */
$target_path $target_path basename$_FILES['uploadedfile']['name']); 
$_FILES['uploadedfile']['tmp_name'];  

$target_path "uploads/";

$target_path $target_path basename$_FILES['uploadedfile']['name']); 

if(
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo 
"The file ".  basename$_FILES['uploadedfile']['name']). 
    
" has been uploaded";
} else{
    echo 
"There was an error uploading the file, please try again!";
}
?>
Be sure to create a folder by the name "uploads" where uploaded files should be put.
And yes, this is a very simple and vulnerable script. You will have to secure it. I don't use this script on my website. It for my hobby use on localhost machine.
Last Blog Entry: Cross browser nuisance (Feb 11th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jan 27th, 2008, 08:44
Jack Franklin's Avatar
Moderator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,409
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 Posts
Re: Image Upload with PHP/MySQL

I use one similar to Rohan. But mine first checks to see if the user is logged in, therefore making it more secure.
__________________
Jack Franklin - Webforumz Moderator
(x)HTML | CSS | PHP | MySQL | JQuery (Javascript)
Contact: My Blog | Twitter | Delicious
Want Lessons? PM me.
If you think I've helped, please press the 'Thanks' Button.
Last Blog Entry: A Week with VBulletin (Aug 28th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Jan 28th, 2008, 23:24
Junior Member
Join Date: Mar 2007
Location: indiana
Age: 25
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Image Upload with PHP/MySQL

Thanks for the replies everybody!

I actually found one that works (it's very similar to yours rohan, just the basics), except it loads the image info into a database, so I can call it with php). It is very insecure though. I actually found it on about.com of all places. I'm sick today, but I'll post it when I return to work on tuesday.

I'll probably use the w3c schools tutorial to add a few restrictions (thanks monie). I'm surprised that I couldn't find a tutorial that clearly combined the upload ability with the mysql info storage, security and file restrictions, but with your suggestions I think I'll be able to piece it all together.

jackfranklin, could you let me know about your security measures?

Thanks again, everyone!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Jan 31st, 2008, 17:08
Junior Member
Join Date: Mar 2007
Location: indiana
Age: 25
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Re: [SOLVED] Image Upload with PHP/MySQL

Here's the code I used for my image upload. I hope it helps someone else.

I'm only using one table in my database. And you must create the images folder on your server before you can use this, or else you will get errors.

Good luck! I'm running into a different problem now though... I've got another post up in this forum called "Image UPLOAD problem". Any help will be greatly appreciated!



PHP: Select all

            <?php
                
//This is the directory where images will be saved 
                
$target "../images/"
                
$target $target basename$_FILES['photo']['name']); 

                
                if (isset(
$_POST['submit'])) {
                
// connect to database
                
include("../connect.php");
                
                
$name $_POST['name'];
                
$since $_POST['since'];
                
$website $_POST['website'];
                
$element $_POST['element'];
                
$pic = ($_FILES['photo']['name']); 

               
//Trims the values to clean up the text
                
trim ($name);
                
trim ($website);
                
trim ($element);
            
               
//adding in escape characters for the query
                
$name addslashes ($name);
                
$website addslashes ($website);
                
$element addslashes ($element);
            
               
//define the query
               
$query "INSERT INTO sponsors (sponsor_id, name, website, element, since, photo) VALUES (0, '$name', '$website', '$element', '$since', '$pic')";

                
//executes the query
               
if (@mysql_query ($query)) { 
            
?>
                 <p>The sponsor was successfully saved.</p>
            <?php 
                    
//Writes the photo to the server 
                    
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target) or die (mysql_error())) 
                    { 
                    
                    
//Tells you if its all ok 
                    
echo "<p>The file "basename$_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory</p>"
                    } 
                    else { 
                    
                    
//Gives an error if its not 
                    
echo "<p>Sorry, there was a problem uploading your file.</p>"
                    }
               } else { 
            
?>
                 <p>The sponsor could not be saved.</p>
            <?php 
               
}
            
                
mysql_close(); //Closes our SQL session
                
} else {
            
?>
                 <p>There is no data to process.</p>
            <?php
                
}
            
?>
            <form action="index.php" method="post">
            <p><input type="submit" name="submit" value="Return to Update Page" /></p>
            </form>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Image Upload Problem kidreapertronV PHP Forum 1 Jan 31st, 2008 18:38
[SOLVED] Php/Mysql Image Problem longstand PHP Forum 6 Oct 17th, 2007 17:27
Upload image file to MySQL as BLOB csun PHP Forum 4 Aug 22nd, 2007 13:14
asp file upload script having problems with MYSQL paulmcn Classic ASP 2 Oct 5th, 2005 18:45
file upload asp-mysql database fluff Classic ASP 7 Aug 19th, 2004 10:45


All times are GMT. The time now is 20:24.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved