Jump to content

images


Ruth

Recommended Posts

I'm trying to allow a user to upload images and I getting the following errors:

 

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\phpD9.tmp' to 'C:/projects/pratice/WWW/ch07/images/' in C:\projects\pratice\WWW\ch07\check_image.php on line 17

 

According to everything I've read it has something to do with the path, but I'm unsure how to fix it.

 

Before I post my code below I ask that you not assume, if you don't understand something please ask.

 

<?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 (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 <br />";

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><br /><br />

<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><br />

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

It is <?php echo $width;?> pixels wide

and <?php echo $height;?> pixels high.<br />

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

</body>

</html>

 

 

Link to comment
https://forums.phpfreaks.com/topic/99025-images/
Share on other sites

lol  ;D  I know that but I'm not sure what the right path is. I'm following along with a book that is kinda like follow me then try it yourself. The show the path as c:/program files/apache group/apache2/test/images. I tired to follow this path manually and could not find apache group, i tired the path anyway and get the same error.

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

in htdocs create a folder called images put the images in the folder

then use this script u made but make sure it also in htdocs

 

<?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 = "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>

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

try

 

<?php
$uploaddir = '';
$uploadfile = $uploaddir . basename($_FILES['upfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile)) {
   echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?> 

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

This is what I get when I tired your suggestion

 

Possible file upload attack!

Here is some more debugging info:Array

(

    [image_filename] => Array

        (

            [name] => jim_carrey.jpg

            [type] => image/jpeg

            [tmp_name] => C:\xampp\tmp\phpDB.tmp

            [error] => 0

            => 7938

        )

 

)

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

try this quickly in htdocs and a folder caled images......

<?php


$uploaddir = 'images/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

?>

<form enctype="multipart/form-data" action=" " method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>

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

I'm confused about the htdocs folder, what does it have to do with my problem? I have a form that is before the code I posted ealier. Here is the form

 

 

<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>

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

I tired this

 

$uploaddir = 'images/';

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

 

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {

    echo "File is valid, and was successfully uploaded.\n";

} else {

    echo "Possible file upload attack!\n";

}

 

Along with the error messages I get Possible file upload attack!

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

OK  same function as before but with a different function

 

 

try:

 

 

<?php

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']);
} else {
   echo "Possible file upload attack: ";
   echo "filename '". $_FILES['userfile']['tmp_name'] . "'.";
}

?> 

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

I've tired

 

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']);

} else {

  echo "Possible file upload attack: ";

  echo "filename '". $_FILES['userfile']['tmp_name'] . "'.";

}

 

 

and get Possible file upload attack: filename ''.

Link to comment
https://forums.phpfreaks.com/topic/99025-images/#findComment-506751
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.