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
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

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?

http://spirf.com/user/demo.php

Upload Code:

Code:
<?php

// Connect to database

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

// 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)
//CREATE;
//@mysql_query($q);

// 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.
       move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");
       $instr = fopen("latest.img","rb");
       $image = addslashes(fread($instr,filesize("latest.img")));
       if (strlen($instr) < 149000) {
               mysql_query ("insert into pix (title, imgdata) values (\"".
               $_REQUEST[whatsit].
               "\", \"".
               $image.
               "\")");
       } 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:

Code:
<form enctype="multipart/form-data" method="post" action="demo.php">
   <p>
 <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">
 <br>
     Please enter the title of that picture:
 <input name="whatsit">
 <br>
     then:
 <input type="submit">
   </p>
 </form>


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 www.opentalk.org.uk 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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho