Uploading images to a website using PHP
Posted by JimL (JimL), 24 February 2004I am trying to develop a website for a friend who knows nothing about HTML.
Therefore to make things easy for him, I am going to write the site in PHP and store his content in a MySQL database.
Using simple admin pages, he can type his content into textboxes, and this information can then be inserted into the MySQL database. The PHP pages will then automatically assemble his content from the MySQL database into the main structure of the webpages.
Fairly simple, and I know how to do this.
However, how can I handle his images?
Ideally I would like to write a script which would upload his images from his harddisk to the webserver. The PHP pages would then read the contents of the "images" folder on the webserver and insert the appropriate code into his web pages automatically.
Can PHP do this? Can it do the job of FTP? Can it read the contents of the "images" folder on the webserver? What about logins to get access to the web space on the webserver?
Alternatively is it possible to store the images in the MySQL database and somehow access them from the HTML code? (This sounds extremely unlikely to me).
Posted by admin (Graham Ellis), 24 February 2004Yes - you can do this with PHP.
I have some sample scripts back in the office; I'll be there towards the end of the week and I can post a follow up then. Please let me know if you find an example elsewhere in the meantime
Posted by JimL (JimL), 25 February 2004Excellent!
I'll look forward to seeing them!
Just to make the problem one stage harder, his photos will be quite large (they are shots taken during motor races). Could PHP be able to manipulate those images to produce thumbnails?
Posted by admin (Graham Ellis), 5 March 2004Hope to have a full answer / sample here within the next 24 hours - will answer you on "thumbnails" too but that one's not so straightforward!
Posted by admin (Graham Ellis), 7 March 2004Sample code to upload an image - tested and it works!
One page to both provide the offering form, and also to do the upload.
All files that are likely to be ASCII are "fixed" for Unix to Windows and vice versa line ends.
Limit applied to file upload size
You need to be VERY CAREFUL what you allow people to upload - this script is NOT on our live server
Posted by admin (Graham Ellis), 7 March 2004On Thumbnails ... I learn something new every day. There's a function called imagecopyresized (and another called imagecopyresampled) that will do the job for you - see the PHP manual pages for details. Here's a sample piece of code I used to test with
and you can run it and see a thumbnail via http://www.wellho.net/demo/thumb.php4
Posted by JimL (JimL), 8 March 2004Fantastic!
I'll heed your warning and make sure that the upload script is restricted only to the owner of the website.
Regarding the thumbnails, I'll give that script a test too!
Thanks very much!
Posted by Razit (Razit), 2 January 2007Hello.
I started writing PHP and using MySQl fairly recently and have no prior experience in programming other than HTML, so my knowledge and understanding of it is still very limited.
Before I continue, I'd like to thank you for this site. I only just found it two days ago, but so far it has been far greater than most other I've found, which has been either too old or too complicated for a beginner.
I found your scripts for image upload and dynamic viewing, both this one and the one found at http://www.wellho.net/solutions/php-example-php-form-image-upload-store-in-mysql-database-retreive.html
I did manage to get that one working, but it does not do exactly what I want it to do, and I can't figure out a way to change it. I tried this one as well, but I couldn't even get it to work.
What I want it to do is simply to enable me to upload images, and then display not only the latest one but all the others, as well. A fairly simple idea, but not quite as simple to execute on my level of expertise, which is basically far below where you can start saying you have a level of expertise.
Currently, my version of your script looks like this:
And the form:
(I chose to leave out the connecting part, but there is one, so don't bother with that part)
What I would like now is a document that don't use the latest.img but is capable of viewing the last uploaded picture and uploading new ones, and another that displays every image in the database.
How would I go about to create that?
Posted by admin (Graham Ellis), 2 January 2007You may have noticed that the article you referred to links to a running script which shows you the three latest pictures. We publish the source code of that here and it should give you some pointers.
A slightly more complex example - allowing you to search our database of images that are in our own MySQL database - is available too - source code and you can also try it out here
There are also further examples under "using MySQL and PHP together" here
Thanks for your comments ... this particular topic is quite a difficult one, as you need to use a lot of technologies in quite a short script, and if it doens't work it won't always be too easy to find out what's going wrong.
Posted by Razit (Razit), 2 January 2007Thank you, you have given me a lot to read through and try out.
I'll give it a go
Posted by Razit (Razit), 2 January 2007Just one more question for now...
How would I set up the database table to work with the code found here? :
Posted by admin (Graham Ellis), 3 January 2007Excellent question - a vital piece of information for the example
Here's the table description off the live table:
So to create the table (from, say, a mysql client) you would use
The same command could be coded into a script, the table could be created through phpmyadmin or another tool ...
Posted by Razit (Razit), 4 January 2007Quite often when I try out a script of this kind (I now have four or five different ones I am trying out or just trying to get it to work) I get a blank white page with the adress of the script - like this: websiteroot.com/directory/script.php - instead of any result.
Can you by this alone give a clue or three what might be wrong?
An example of a piece of script:
Posted by admin (Graham Ellis), 5 January 2007Sounds like you have some odd error settings and your code might be outputting something before the header. Unless you have a syntax error in your code reproduced below, the clue will be elsewhere. Does your <?php start on row 1, colum 1 of the script? A single space or a blank line in front of it could be the cause.
Posted by Razit (Razit), 5 January 2007No spaces, no new lines. The <?php tag starts right away.
I just remembered another piece of code that gives that result; the piece of your feeder script that should get the filename.
I'd really like to get that part working, and I hoped that this might help further?
Posted by admin (Graham Ellis), 5 January 2007Perhaps you're using an extension that isn't configured to be processed through PHP? And that latest script will certainly need a header(...) added.
Posted by Razit (Razit), 5 January 2007Yes, very true. The script that I posted doesn't even work, it has the header(Content-Type: image/jpeg) missing.
Extension is *.php...
I don't know, maybe this is the kind of thing that can't be solved just like that over a forum. Or maybe it is, only I don't give the right information.
Oh yes, I forgot to mention that it seems to be every time I add the header info that it does the blanky. The only time it doesn't is in the first script I talked about and posted.
Posted by admin (Graham Ellis), 5 January 2007It's probably one of those things I would spot when I saw the system and it would be easier in real life. But looking at the IP address you're posting from, you're not living nearby to pop over, are you?
One of the next tests I would do at this stage is to browse the URL that's giving the problem via telnet - cut out the browser - so that I could see more about what's really happening. Is that a technique you've come across?
Posted by Razit (Razit), 5 January 2007Uhm... no, not really. Not unless you're prepared to pop on a plane first. I'll just have to try and solve it as best I can by experimenting with what you have suggested
I have heard of it, certainly, but never actually used it, or even got a good grasp of exactly what it does. I use a client to connect to my server via SSH, which I have been told is something similar, but that is as far as my knowledge goes.
Could you perhaps give a suggestion of a good client to use?
Posted by Razit (Razit), 5 January 2007Actually, I think I'm on to the problem now, I just can't figure out a solution.
The thing is that most of these scripts have been put on pages with a lot of html in them as well. Usually that isn't a problem, but whenever I try to put in a header(...) in the script it starts acting up in the way I desribed. I tried a number of different ways to output the html now, and nothing seems to work.
Here is the full code of a page (or at least one of my attempts at it) with a script that I know work without any html, but as soon as I put it in it does the blank page instead (am I wrong in supposing that the headers collide in som way?):
Do you have any thoughts on that?
Posted by admin (Graham Ellis), 6 January 2007Ah!
A URL can serve out either an image, or a piece of HTML and not both, at a single call. You need to use two different URLs or, you could if your're clever about it, use the same page name with different parameters.
In the HTML output, include an img tag with the src pointing to the second URL.
Posted by Razit (Razit), 6 January 2007Thanks!
After a little bit of trixing around I made it work.
Now maybe I can make some of my other attempted scripts work as well!
I have yet to make your feeder.php script work, though But that's another story; I'm not entirely sure my server is correctly configured for it.
Again, thanks for the help!
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho