Jump to content

images


Ruth

Recommended Posts

:D ok

HTML

<html>
<head>
<title>Upload your pic to our site!</title>
</head>
<body>
<form name = "form1" method = "post" action = "check_image.php"
enctype = "multipart/form-data">

<table border = "0" cellpadding = "5">
<tr>
	<td>Image Title or Caption<br />
		<em>Example: You talkin' to me?</em></td>
	<td>
		<input name="image_caption" type="text" id="item_caption"
		size="55" maxlength="255">
	</td>
</tr>

<tr>
	<td>Your Username</td>
	<td>
		<input name="image_username" type="text" id="image_username"
		size="15" maxlength="255">
	</td>
</tr>

<tr>
	<td>Upload Image:</td>
	<td>
		<input name="image_filename" type="file" id="image_filename">
	</td>
</tr>
</table>
<br />
<em>Acceptable image formats include: GIF, JPG/JPEG, and PNG.</em>
<p align="center"><input type="submit" name="Submit" value="Submit">
 
<input type="reset" name="Submit2" value="Clear Form">
</p>
</form>
</body>
</html>

 

PHP

<?php
//connect to db
require_once('dbconnection.php');

//added suggestion
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
   echo "File ". $_FILES['userfile']['name'] ." uploaded successfully.\n";
   echo "Displaying contents\n";
   readfile($_FILES['userfile']['tmp_name']);


//pull var from form
$image_caption = $_POST['image_caption'];
$image_username = $_POST['image_username'];
$image_tempname = $_POST['image_filename'];
$today = date("Y-m-d");

//upload image and check for image type
//path not working
$imageDir = "C:/projects/pratice/WWW/ch07/images/";
$imageName = $imageDir . $image_tempname;

//check to see if file is valid upload file (uploaded by post)
if (move_uploaded_file($_FILES['image_filename']['tmp_name'], $imageName))
{
   //get info about the image being uploaded
   list ($width, $height, $type, $attr) = getimagesize($imageName);
   
   switch ($type)
   {
      case 1:
         $ext = ".gif";
         break;
         
      case 2:
         $ext = ".jpg";
         break;
         
      case 3:
         $ext = ".png";
         break;
         
      default:
         echo "Sorry, but the file you uploaded was not a GIF,
              JPG, or PNG file
";
         echo "Please hit your browser's 'back' button and try again";
   }
   
   //insert info into image table
   $insert = "INSERT INTO images
            (image_caption, image_username, image_date)
            VALUES
               ('$image_caption', '$image_username', '$today')";
   $insertResults = mysql_query($insert) or die (mysql_error());
   
   $lastpicid = mysql_insert_id();
   
   $newfilename = $imageDir . $lastpicid . $ext;
   
   rename ($imageName, $newfilename);
}
?>
<html>
<head>
   <title>Here is your pic</title>
</head>
<body>
<h1>So how does it feel to be famous</h1>


<p>Here is the picture you just uploaded to our servers:</p>
<img src = "images/<?php echo $lastpicid . $ext;?>" align="left">
<strong><?php echo $image_name;?></strong>

This image is a <?php echo $ext;?> image.

It is <?php echo $width;?> pixels wide
and <?php echo $height;?> pixels high.

It was uploaded on <?php echo $today;?>.
</body>
</html>

 

EDITED BY WILDTEEN88: Please use code tags (


) when posting code.

Link to comment
https://forums.phpfreaks.com/topic/99025-images/page/2/#findComment-506787
Share on other sites

TRry:

 

<?php
//connect to db
require_once('dbconnection.php');




//pull var from form
$image_caption = $_POST['image_caption'];
$image_username = $_POST['image_username'];
$image_tempname = $_POST['image_filename'];
$today = date("Y-m-d");

//upload image and check for image type
//path not working
$imageDir = "C:/projects/pratice/WWW/ch07/images/";
$imageName = $imageDir . $image_tempname;

//check to see if file is valid upload file (uploaded by post)
if (is_uploaded_file($imageName)){
   list ($width, $height, $type, $attr) = getimagesize($imageName);
   
   switch ($type)
   {
      case 1:
         $ext = ".gif";
         break;
         
      case 2:
         $ext = ".jpg";
         break;
         
      case 3:
         $ext = ".png";
         break;
         
      default:
         echo "Sorry, but the file you uploaded was not a GIF,
              JPG, or PNG file
";
         echo "Please hit your browser's 'back' button and try again";
   }
   
   //insert info into image table
   $insert = "INSERT INTO images
            (image_caption, image_username, image_date)
            VALUES
               ('$image_caption', '$image_username', '$today')";
   $insertResults = mysql_query($insert) or die (mysql_error());
   
   $lastpicid = mysql_insert_id();
   
   $newfilename = $imageDir . $lastpicid . $ext;
   
   rename ($imageName, $newfilename);
}
?>
<html>
<head>
   <title>Here is your pic</title>
</head>
<body>
<h1>So how does it feel to be famous</h1>


<p>Here is the picture you just uploaded to our servers:</p>
<img src = "images/<?php echo $lastpicid . $ext;?>" align="left">
<strong><?php echo $image_name;?></strong>

This image is a <?php echo $ext;?> image.

It is <?php echo $width;?> pixels wide
and <?php echo $height;?> pixels high.

It was uploaded on <?php echo $today;?>.
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/99025-images/page/2/#findComment-506795
Share on other sites

Warning: move_uploaded_file(C:/projects/pratice/WWW/ch07/images/) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\projects\pratice\WWW\ch07\check_image.php on line 17

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\phpDF.tmp' to 'C:/projects/pratice/WWW/ch07/images/' in C:\projects\pratice\WWW\ch07\check_image.php on line 17

Link to comment
https://forums.phpfreaks.com/topic/99025-images/page/2/#findComment-506798
Share on other sites

I hit the motherload

 

Undefined index: image_filename in C:\projects\pratice\WWW\ch07\check_image.php on line 9

 

Undefined variable: lastpicid in C:\projects\pratice\WWW\ch07\check_image.php on line 65

 

Undefined variable: ext in C:\projects\pratice\WWW\ch07\check_image.php on line 67

 

Undefined variable: width in C:\projects\pratice\WWW\ch07\check_image.php on line 68

 

Undefined variable: height in C:\projects\pratice\WWW\ch07\check_image.php on line 69

Link to comment
https://forums.phpfreaks.com/topic/99025-images/page/2/#findComment-506805
Share on other sites

$lastpicid is not set

$width not set

$height not set

$image_tempname not set

$ext not set

 

I know the height and width aren't being set because they don't even hit the list statement that sets them. Same with the $ext. Let me check one more then regarding the $lastpicid. I'm not sure why the image_tempname is not being set

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/99025-images/page/2/#findComment-506827
Share on other sites

<?php
//connect to db
require_once('dbconnection.php');




//pull var from form
$image_caption = $_POST['image_caption'];
$image_username = $_POST['image_username'];
$image_tempname = $_FILES['image_filename']['name'];

$today = date("Y-m-d");


//make sure  posts are set



if(isset($_POST['image_username'])||isset($_POST['image_caption'])) {


//upload image and check for image type
//path not working
$imageDir = "C:/projects/pratice/WWW/ch07/images/";
$imageName = $imageDir . basename($_FILES


//check to see if file is valid upload file (uploaded by post)
if (is_uploaded_file($_FILES{
   list ($width, $height, $type, $attr) = getimagesize($imageName);
   
   switch ($type)
   {
      case 1:
         $ext = ".gif";
         break;
         
      case 2:
         $ext = ".jpg";
         break;
         
      case 3:
         $ext = ".png";
         break;
         
      default:
         echo "Sorry, but the file you uploaded was not a GIF,
              JPG, or PNG file
";
         echo "Please hit your browser's 'back' button and try again";
   }
   
   //insert info into image table
   $insert = "INSERT INTO images
            (image_caption, image_username, image_date)
            VALUES
               ('$image_caption', '$image_username', '$today')";
   $insertResults = mysql_query($insert) or die (mysql_error());
   
   $lastpicid = mysql_insert_id();
   
   $newfilename = $imageDir . $lastpicid . $ext;
   
   rename ($imageName, $newfilename);
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/99025-images/page/2/#findComment-506837
Share on other sites

:o I got it fixed I need to work some bugs out but heres what I did

 

I changed this $image_tempname = $_FILES['image_filename']['name'];

 

and changed my if statement back to this

 

if (move_uploaded_file($_FILES['image_filename']['tmp_name'], $imageName))

 

I don't know if I should laugh or cry all this trouble because I missed type a var.

 

Thank you so much for your help I'm really grateful

Link to comment
https://forums.phpfreaks.com/topic/99025-images/page/2/#findComment-506847
Share on other sites

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.