Jump to content

Opening Uploaded File


vital101

Recommended Posts

Hey everyone,

 

I'm having some difficulty opening an image file that has been uploaded from a form.  I upload the file (that works fine), but then when I go to open it, I have no luck.  I keep thinking that maybe my path to the image is wrong, but i don't think it is.  Any help would be awesome.  Thanks!

 

<?php
//Create SQL Connect, test for connectivity.
$con = mysql_connect("", "myuser", "mypass");
if (!$con) {
	die("An error has occurred connecting to the database.");
}
//Select the database to use.
mysql_select_db("mydb", $con);

//Construct the query
$query = "SELECT * FROM images";
$result = mysql_query($query);
$image_number = mysql_num_rows($result) + 1;
$id_number = mysql_num_rows($result) + 1;
$image_number = $image_number . ".jpg";

//Target is the path and filename where the image will be stored.
$target = "uploads/$image_number";
//$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;

//This is our limit file type condition
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}

//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
echo "Sorry your file was not uploaded";
}

//If everything is ok we try to upload it
else
{
	move_uploaded_file($_FILES['uploaded']['tmp_name'], $target);
}

//Open the image stream.
$image = open_image('/www/facebook/morningAfter/images/$image_number');

//Check to make sure image stream opened.
       /*****  THIS IS WHERE IT BREAK!  *****/
if ($image === false) {
		die ('Unable to open image');
}

//Dynamically resize images down to a width of 125px or less
if($width > 125) {	
	$width = imagesx($image);
	$height = imagesy($image);
	$ratio = 125 / $width;
	$new_width = 125;
	$new_height = (int)($height * $ratio);

	// Resample
	$image_resized = imagecreatetruecolor($new_width, $new_height);
	imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

	imagejpeg($image_resized, "/www/facebook/morningAfter/images/$image_number", 83);
	imagedestroy($image);
	imagedestroy($image_resized);
	echo "done!";
} else {
	imagejpeg($image, "/www/facebook/morningAfter/images/$image_number", 83);
	imagedestroy($image);
}

//Now, insert this new beauty into the database of images.  But first, get the timestamp.
$currentTime = time();
$formattedTime = gmdate("Y-m-d H:i:s", $currentTime);

$query = "INSERT INTO images (id, name, date_added, popularity) VALUES ($id_number, '$image_number', '$formattedTime', 0)";
mysql_query($query);

//Great! Forward the user on to selecting a friend.
header('Location: http://apps.facebook.com/morningafter/upload_complete_catcher.php?itemID=$id_number');

/* Begin Custom Functions */

function open_image ($file) {
        # JPEG:
        $im = @imagecreatefromjpeg($file);
        if ($im !== false) { return $im; }

        # GIF:
        $im = @imagecreatefromgif($file);
        if ($im !== false) { return $im; }

        # PNG:
        $im = @imagecreatefrompng($file);
        if ($im !== false) { return $im; }

        # GD File:
        $im = @imagecreatefromgd($file);
        if ($im !== false) { return $im; }

        # GD2 File:
        $im = @imagecreatefromgd2($file);
        if ($im !== false) { return $im; }

        # WBMP:
        $im = @imagecreatefromwbmp($file);
        if ($im !== false) { return $im; }

        # XBM:
        $im = @imagecreatefromxbm($file);
        if ($im !== false) { return $im; }

        # XPM:
        $im = @imagecreatefromxpm($file);
        if ($im !== false) { return $im; }

        # Try and load from string:
        $im = @imagecreatefromstring(file_get_contents($file));
        if ($im !== false) { return $im; }

        return false;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/101985-opening-uploaded-file/
Share on other sites

Yes, the file does exist.  That's why I can't figure out what's wrong.  When opening the file, I've tried using absolute paths, relative paths, and everything in between, with no luck.  My directory structure looks something like this:

 

-/home/theloobc/

  -/www

    -/facebook

      -/morningAfter

        -/images

        -/uploads

Well, then I'm out of suggestions.

 

Here is what I would do if I were in a situation like that.

1. I would upload an image of a known quality/format to a location on the webserver.

2. Create a brand new script that trys to open the image

<?php
$path = '/path/to/your/image.jpg';
$result = imagecreatefromjpeg($path);
var_dump($result);
?>

3. Look at the output from this script and see if it works

 

 

Also I noticed that on your original script you are using the '@' char for error suppression. You should remove that while you are debugging.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.