Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

move_uploaded_file(images/) [function.move-uploaded-file]: failed to open stream

stream

  • Please log in to reply
21 replies to this topic

#13 Julien Duponte

Julien Duponte

    CC Newcomer

  • Just Joined
  • PipPip
  • 19 posts

Posted 27 April 2011 - 10:33 AM

My brain has officially melted...
Okay, this is where I am at. I have a photodisplay.php that I use to call images ?id=etc. My display code is this:

<?php


$username = "y";
$password = "";
$host = "localhost";
$database = "yakimaimplement";

mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

mysql_select_db($database) or die("Can not select the database: ".mysql_error());

$data = mysql_query("SELECT * FROM used_equip")
or die(mysql_error());

//Outputs the image and other data
//Need to add a LOOP of some sort, to tile entries down page.

while($info = mysql_fetch_array( $data ))
{
Print "<table>";
Print "<tr valign=\"top\">";
Print "<td rowspan=\"3\">";
Print "<img src=\"".$info['photo'] . \">";
Print "</td>";
Print "<td style=\"padding-left: 20px;\">";
Print "<b>Model:</b> ".$info['model'] . "<br> ";
Print "<b>Serial:</b> ".$info['serial'] . "<br> ";
Print "<b>Application:</b> ".$info['application'] . "<br> ";
Print "<br />";
Print "</p>";
Print "</td>";
Print "</tr>";
Print "</table>";
}
?>

I did something, which probably could have been accomplished easier some other way, but I have a field in the images table that matches a field in the used_equip table, so that (I assume)when I display each item, it matches with the correct image.

In my head, I keep wanting to do something like "SELECT * FROM used_equip,images WHERE serial LIKE usedid".
And then, in my array_fetch, it will ensure that they are matched. However, that makes no sense whatsoever. I think I'm approaching it wrong.

I don't know much about the JOIN function, is that what I want?
Would I do a SELECT usedid FROM images and then have a loop that matches the two fields, then processes the photo with each while loop that way?

Endless frustration.
  • 0

#14 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 27 April 2011 - 07:11 PM

I believe you could just do something like this:
SELECT * FROM used_equip JOIN ON images USING (usedid);
//or if ids are not both named "usedid":
SELECT * FROM used_equip JOIN ON images WHERE used_equip.id = images.usedid
You would need to store each image with a matching usedid that matches the used_equip id that it belongs to, this provides associations between tables.

Here is an informative tutorial on joins: MySQL Table Joins in MySQL

Also, if you are not already, you may wish to use something such as PHPMyAdmin (if you have it) or your terminal with mysqladmin if it is local, to try queries to see if they are right. It is a lot easier than testing queries with PHP (and modifying all the code and then modifying again to see if it works)
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#15 Julien Duponte

Julien Duponte

    CC Newcomer

  • Just Joined
  • PipPip
  • 19 posts

Posted 27 April 2011 - 07:55 PM

Thank you again, Alexander. I decided that the fact that I had two tables was tedious and unnecessary.

I have everything working except this final part.

This is image.php, from elsewhere that I am using:

<?php
	/**
	 * Display image form database
	 *
	 * Retrive an image from mysql database if image id is provided.
	 *
	 * @example to display a image with image id 1, place <img src="image.php?id=1" > in your html file.
	 *
	 * @author Md. Rayhan Chowdhury
	 * @copyright www.raynux.com
	 * @license LGPL
	 */

	// verify request id.
	if (empty($_GET['id']) || !is_numeric($_GET['id'])) {
		echo 'A valid image file id is required to display the image file.';
		exit;
	}

	$imageId = $_GET['id'];

	//connect to mysql database
	if ($conn = mysqli_connect('localhost', 'root', 'root', 'test')) {
		$content = mysqli_real_escape_string($conn, $content);
		$sql = "SELECT type, content FROM images where id = {$imageId}";

		if ($rs = mysqli_query($conn, $sql)) {
			$imageData = mysqli_fetch_array($rs, MYSQLI_ASSOC);
			mysqli_free_result($rs);
		} else {
			echo "Error: Could not get data from mysql database. Please try again.";
		}
		//close mysqli connection
		mysqli_close($conn);

	} else {
		echo "Error: Could not connect to mysql database. Please try again.";
	}	

	if (!empty($imageData)) {
		// show the image.
		header("Content-type: {$imageData['type']}");
		echo $imageData['content'];
	}
?>

And,
This is the code to loop/display each entry:

<?php


$username = "";
$password = "";
$host = "localhost";
$database = "yakimaimplement";

mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

mysql_select_db($database) or die("Can not select the database: ".mysql_error());

$data = mysql_query("SELECT model,serial,application FROM used_equip")
or die(mysql_error());

//Outputs the image and other data
//Need to add a LOOP of some sort, to tile entries down page.

while($info = mysql_fetch_array( $data ))
{
Print "<table>";
Print "<tr valign=\"top\">";
Print "<td rowspan=\"3\">";
[B]Print "<img src=\"image.php?id=$imageId \">";[/B]
Print "</td>";
Print "<td style=\"padding-left: 20px;\">";
Print "<b>Model:</b> ".$info['model'] . "<br> ";
Print "<b>Serial:</b> ".$info['serial'] . "<br> ";
Print "<b>Application:</b> ".$info['application'] . "<br> ";
Print "<br />";
Print "</p>";
Print "</td>";
Print "</tr>";
Print "</table>";
}
?>

The bold part is where I'm having trouble. I recognize that that is silly.. I need to create a loop somehow. I can't figure out. I'm going to keep at it. This is the final step. AFTER THIS, I am done. And, gosh, I need to send you money for saving my life :)
  • 0

#16 Julien Duponte

Julien Duponte

    CC Newcomer

  • Just Joined
  • PipPip
  • 19 posts

Posted 27 April 2011 - 08:12 PM

Oh... wait. What if I did something like this:
Print "<img src=\"image.php?id=".$info['id'] . "\">";


It's not working :) But, I feel like I'm in the right direction?
  • 0

#17 Julien Duponte

Julien Duponte

    CC Newcomer

  • Just Joined
  • PipPip
  • 19 posts

Posted 27 April 2011 - 09:05 PM

Well, it's not working. I'm assuming this has something to do with trying to run the file through php? That I can't send multiple headers? I can't figure it out. When I view the image info, it shows up as the correct link. So, I'm at a loss. Gah.
  • 0

#18 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 27 April 2011 - 11:45 PM

Your code seems correct where you add the id dynamically with the HTML img tag, as the client's browser would make n separate requests to image.php to get the images.

On the image.php, any text will make the image become a blank image or error symbol. There must be something preventing it from showing, either an error is being displayed (that would break the image), whitespace i.e. a space or byte order mark if the php file is stored as utf and not ascii breaks it, or the query does not work. I am not 100% sure my query would provide the correct result, so it would be wise to test it in a testing file so you can be sure the binary data is echo'd without an error, and after, then use the header() command again to see if it can display a proper image.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#19 Julien Duponte

Julien Duponte

    CC Newcomer

  • Just Joined
  • PipPip
  • 19 posts

Posted 27 April 2011 - 11:52 PM

Hmm. I doubled checked and made an .html file that just had img codes in it and it printed out the images. So, the problem is definitely happening somewhere in the middle. It's possible that it's whitespace. I'll play around with that a little. Here's my image.php file again, just in case you see anything, otherwise I'll keep dinking around. I know I'm close, but I'm not getting any errors, other than the image not showing up.

[code=auto:0][<?php
/**
* Display image form database
*
* Retrive an image from mysql database if image id is provided.
*
* @example to display a image with image id 1, place <img src="image.php?id=1" > in your html file.
*
* @author Md. Rayhan Chowdhury
* @copyright www.raynux.com
* @license LGPL
*/

// verify request id.
if (empty($_GET['id']) || !is_numeric($_GET['id'])) {
echo 'A valid image file id is required to display the image file.';
exit;
}

$imageId = $_GET['id'];

//connect to mysql database
if ($conn = mysqli_connect('localhost', 'yakimaimplement', 'password', 'yakimaimplement')) {
$content = mysqli_real_escape_string($conn, $content);
$sql = "SELECT type, content FROM usedequip where id = {$imageId}";

if ($rs = mysqli_query($conn, $sql)) {
$imageData = mysqli_fetch_array($rs, MYSQLI_ASSOC);
mysqli_free_result($rs);
} else {
echo "Error: Could not get data from mysql database. Please try again.";
}
//close mysqli connection
mysqli_close($conn);

} else {
echo "Error: Could not connect to mysql database. Please try again.";
}

if (!empty($imageData)) {
// show the image.
header("Content-type: {$imageData['type']}");
echo $imageData['content'];
}
?>/CODE]
  • 0

#20 Julien Duponte

Julien Duponte

    CC Newcomer

  • Just Joined
  • PipPip
  • 19 posts

Posted 27 April 2011 - 11:56 PM

p.s. In your opinion, do you think that I should just have the file uploaded and the file link saved in the database so that I can just call a text link? They aren't going to have a lot of data, so I'm not too worried about the server getting filled up with data or anything. Which a lot of the anti-image-> db people mention. It's just that this method 'seemed' like the best option now that I have it working. So, I kind of want to just make this work.

Anyways. Still tinkering.
  • 0

#21 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 28 April 2011 - 12:05 AM

I believe your script contains no errors, although I would agree storing text (and not error-prone binary) to be the most efficient. Although caching exists, if the client were to request (a new client actually) the image it would provide more significant load than if they were to just load the image from a directoriy (/images/{usedid}.jpg).

It is easier to manage files, and storing images in a database was really meant for offline programs. Servers are very efficient at serving normal files and will likely be automatically cached, your dynamic ones may not unless you set expires headers, as it believes it is a dynamic page and not an image.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#22 Julien Duponte

Julien Duponte

    CC Newcomer

  • Just Joined
  • PipPip
  • 19 posts

Posted 28 April 2011 - 03:09 PM

Well, I gave up on that. I went over and over the code. Tried quite a number of things, but I can neither generate an error nor figure out where the problem is coming from.
I secretly assume that it does have to do with the headers. But, I want ahead and went the route of one table, uploading the image, and sending the link the to db. Which, I tried originally and was at a loss, this time around I had it running in no time. Just wanted to say thank you for all your help, it's very appreciated!
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download