Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
MySQL Upload Script

Posted by noah (noah), 13 May 2006
I have used your script to upload images to a MySQL database, however it doesn't seem to work, I get a little box with an "X" in it and a MySQL error.  Any suggestions?

Upload Code:


// Connect to database

$errmsg = "MySQL Error, Please check your syntax and try again";
if (! @mysql_connect("localhost","nhwood_testing","*******")) {
       $errmsg = "Cannot connect to database";

// First run ONLY - need to create table by uncommenting this
// Or with silent @ we can let it fail every sunsequent time ;-)

//$q = <<<CREATE
//create table pix (
//    pid int primary key not null auto_increment,
  // title text,
 //  imgdata longblob)

// Insert any new image into database

if ($_REQUEST[completed] == 1) {
       // Need to add - check for large upload. Otherwise the code
       // will just duplicate old file ;-)
       // ALSO - note that latest.img must be public write and in a
       // live appliaction should be in another (safe!) directory.
       $instr = fopen("latest.img","rb");
       $image = addslashes(fread($instr,filesize("latest.img")));
       if (strlen($instr) < 149000) {
               mysql_query ("insert into pix (title, imgdata) values (\"".
               "\", \"".
       } else {
               $errmsg = "Too large!";

// Find out about latest image

$gotten = @mysql_query("select * from pix order by pid desc limit 1");
if ($row = @mysql_fetch_assoc($gotten)) {
       $title = htmlspecialchars($row[title]);
       $bytes = $row[imgdata];
} else {
       $errmsg = "There is no image in the database yet";
       $title = "no database image available";
       // Put up a picture of our training centre
       $instr = fopen("../bar.png","rb");
       $bytes = fread($instr,filesize("../bar.png"));

// If this is the image request, send out the image

if ($_REQUEST[gim] == 1) {
       header("Content-type: image/jpeg");
       print $bytes;
       exit ();

Form Code:

<form enctype="multipart/form-data" method="post" action="demo.php">
 <input type="hidden" name="MAX_FILE_SIZE" value="150000">
 <input type="hidden" name="completed" value="1">
     Please choose an image to upload:
 <input type="file" name="imagefile">
     Please enter the title of that picture:
 <input name="whatsit">
 <input type="submit">

Posted by admin (Graham Ellis), 13 May 2006
This is a script that puts together a whole lot of technologies all in the one script, so you need to test and debug section by section, or at the least look at the results on the server when you try to run it but it fails.

My next step in your situation would be to connect into the MySQL database through the MySQL program and check to see whether or not the table had been created, and if it has been corrected whether or not it has the right structure, and if it has the right structure whether it contains rows that look valid.

Looking at your code, I notice that the response page that was on the end of my upload scripts - the HTML generator - is missing.   It's also possible that an error in that code (for example the removal of the parameter on the src= in the img tag) would create a problem as you've described it.

Posted by noah (noah), 13 May 2006
Thanks, I'll give that a try.  I've debugged many times, and most of my scripts fail, I just hoping to fix it.  What do you mean about the "img" tag?

Posted by admin (Graham Ellis), 14 May 2006
Running your program, I note that you have a warning message that you didn't report on your earlier posts. That's more significant than the broken picture link -  you need to  fix the problem reported by the messages

Warning: move_uploaded_file(latest.img): failed to open stream: Is a directory in /home/nhwood/public_html/spirf/user/demo.php on line 111

Warning: move_uploaded_file(): Unable to move '/tmp/php4H191S' to 'latest.img' in /home/nhwood/public_html/spirf/user/demo.php on line 111
Which is related to file permissions first - the uploaded image cannot be written to your intermediate directory, and so isn't getting to the database.

My comment aboout the img tag may now be irrelevant.

Posted by noah (noah), 14 May 2006
I've ignored that becasue I've gave just about all of the directories 777

Posted by admin (Graham Ellis), 14 May 2006
on 05/14/06 at 16:29:06, noah wrote:
I've ignored that becasue I've gave just about all of the directories 777

Well, it's having a problem writing it, isn't it?  Perhaps you've created a directory called latest.img that it can't overwrite with a plain file.  Have a careful look at the error message and see what it says - you already have the clue there  

[Noah - you may be correct in suggesting that my advise is wrong, but as you hadn't told us that you had already set the file permissions, we were at best shooting in the dark.  If you can provide information that's as complete as possible, then we can help as best as possible.  And, b.t.w. it's usually counterproductive to be rude to people who give you there time free to make suggestions to help you along, even if a suggestion turns out to NOT be a solution]

Posted by noah (noah), 14 May 2006
I didn't mean to be rude, Sorry.  I just figured that it would work and I was getting mad at myself.  Anyway I have got it to work!  Thanks for your help!  

Posted by admin (Graham Ellis), 14 May 2006
Fair enough ...  

WAS it a directory called latest.img by any chance?

Posted by noah (noah), 14 May 2006
Yes and no, I did have that directory and removed it, however it still didn't work, I had to make the user directory 777 as well, Thanks a lot!  

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho