Uploading and storing image

hello ,
i am following an uploading and storing image tutorial, but a default message to show that you have to select an image is not showing(echo “Please select an image.”:wink:
the code so far is below thanks

    <?php require_once("include/session.php");?>
    <?php require_once("include/dataconnect.php");?>
    <?php require_once("include/functions.php");?>
    <?php include("include/header.php");?>
    <div>
    <!--<form action='tindex5.php' method='Post' enctype='multipart/form-data'class='rl'>-->
    <form action='affiliatestore.php' method='Post' enctype='multipart/form-data'class='rl'>
        <div>
        <label for='name' class='fixedwidth'>name</label>
        <input type='text' name='name' id='name'/>
        </div>

        <div>
        <label for='namelink' class='fixedwidth'>namelink</label>
        <input type='text' name='username' id='username'/>
        </div>

        <div>
        <label for='image' class='fixedwidth'>image</label>
        <input type='file' name='image' id='image'/>
        </div>

        <div>
        <label for='imagelink' class='fixedwidth'>imagelink</label>
        <input type='text' name='imagelink' id='imagelink'/>
        </div>

        <div>
        <label for='base' class='fixedwidth'>base</label>
        <input type='text' name='base' id='base'/>
        </div>

        <div>
        <label for='baselink' class='fixedwidth'>baselink</label>
        <input type='text' name='baselink' id='baselink' />
        </div>

        <div class='buttonarea'>
                <p>
                <input type='submit' name='submit' value='submit'>
                </p>
                </div>
    </form>
    </div>
    <?php
    //file properties
    $file= $_FILES['image']['tmp_name'];
    if (!isset($file))
    echo "Please select an image.";
else
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if ($image_size==FALSE)
echo "Thats not an image";
else
{
}

}
?>
<?php include("include/footer.php");?>

I think that is caused by poor bracketing made worse by a lack of indenting …


    if (!isset($file)) {
    echo "Please select an image.";
    }else{
    // get on with it 

    }

I only try and get away with stuff like that when there is no else clause:



  if( $loggedin )
          showlogoutbutton();


@cups thanks, please i have followed the tutorial but the image upload is not displaying

this is the code
affiliatestore.php

<?php require_once("include/session.php");?>
<?php require_once("include/dataconnect.php");?>
<?php require_once("include/functions.php");?>
<?php include("include/header.php");?>
<div>
<!--<form action='tindex5.php' method='Post' enctype='multipart/form-data'class='rl'>-->
<form action='affiliatestore.php' method='Post' enctype='multipart/form-data'class='rl'>
	<div>
	<label for='name' class='fixedwidth'>name</label>
	<input type='text' name='name' id='name'/>
	</div>
	
	<div>
	<label for='namelink' class='fixedwidth'>namelink</label>
	<input type='text' name='namelink' id='namelink'/>
	</div>
	
	<div>
	<label for='image' class='fixedwidth'>image</label>
	<input type='file' name='image' id='image'/>
	</div>
	
	<div>
	<label for='imagelink' class='fixedwidth'>imagelink</label>
	<input type='text' name='imagelink' id='imagelink'/>
	</div>
	
	<div>
	<label for='base' class='fixedwidth'>base</label>
	<input type='text' name='base' id='base'/>
	</div>
	
	<div>
	<label for='baselink' class='fixedwidth'>baselink</label>
	<input type='text' name='baselink' id='baselink' />
	</div>
		
	<div class='buttonarea'>
			<p>
			<input type='submit' name='submit' value='submit'>
			</p>
			</div>
</form>
</div>
<?php
//if (isset($_POST['submit'])) {
//file properties
//$file= $_FILES['image']['tmp_name'];
if (!isset($_FILES['image']))
		{
		echo "Please select an image.";
		}
else
			{
	if (isset($_POST['submit']))
  				{
					$image = mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
					$image_name = ($_FILES['image']['name']);
					$namelink = mysql_real_escape_string(htmlentities(strip_tags($_POST['namelink'])));
					$imagelink = mysql_real_escape_string(htmlentities(strip_tags($_POST['imagelink'])));
					$base = mysql_real_escape_string(htmlentities(strip_tags($_POST['base'])));
					$baselink = mysql_real_escape_string(htmlentities(strip_tags($_POST['baselink'])));
					$image_size = getimagesize($_FILES['image']['tmp_name']);
				}
if ($image_size==FALSE)
				echo "Thats not an image";
else
				{
if(!$insert = mysql_query("

					INSERT INTO store VALUES ('','$image_name','$namelink','$image','$imagelink','$base','$baselink')

						"))
						echo"Problem uploading image";
else
				{
					$lastid = mysql_insert_id();
					echo"Image uploaded.<p />Your image:<p /><img src=getaffiliatestore.php?id=$lastid>";
				}
				}

				}
?>

getaffiliatestore.php

<?php require_once("include/session.php");?>
<?php require_once("include/dataconnect.php");?>
<?php require_once("include/functions.php");?>
<?php include("include/header.php");?>
<?php

$id=  mysql_real_escape_string($_REQUEST['id']);
$image = mysql_query("SELECT * FROM store WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;

?>

hello ,
i am following an uploading and storing image tutorial, but a default message to show that you have to select an image is not showing echo(“Please select an image.”

So, is that issue now fixed?

yes the select an image is now fixed, just that, after uploading the image, and the image is meant to show.but the image does not show

Carefully read the manual pages on uploads.

Then use var_dump($_FILES) to look at the value for ‘error’, if that is greater than 0 then that should give you a better idea of what caused the failure.

@cups the error is 0 but i dont why the page is not retirving,

when i <?php print_r($_FILES);?>

i had

Array ( [image] => Array ( [name] => id.jpeg [type] => image/jpeg [tmp_name] => /private/var/tmp/phpZUl2tj [error] => 0 [size] => 463762 ) ) ,

this is the page that is supposed to display the image

 <?php print_r($_FILES);?>
    <div>
    <!--<form action='tindex5.php' method='Post' enctype='multipart/form-data'class='rl'>-->
    <form action='affiliatestore.php' method='Post' enctype='multipart/form-data'class='rl'>
        <div>
        <label for='name' class='fixedwidth'>name</label>
        <input type='text' name='name' id='name'/>
        </div>

        <div>
        <label for='namelink' class='fixedwidth'>namelink</label>
        <input type='text' name='namelink' id='namelink'/>
        </div>

        <div>
        <label for='image' class='fixedwidth'>image</label>
        <input type='file' name='image' id='image'/>
        </div>

        <div>
        <label for='imagelink' class='fixedwidth'>imagelink</label>
        <input type='text' name='imagelink' id='imagelink'/>
        </div>

        <div>
        <label for='base' class='fixedwidth'>base</label>
        <input type='text' name='base' id='base'/>
        </div>

        <div>
        <label for='baselink' class='fixedwidth'>baselink</label>
        <input type='text' name='baselink' id='baselink' />
        </div>

        <div class='buttonarea'>
                <p>
                <input type='submit' name='submit' value='submit'>
                </p>
                </div>
    </form>
    </div>
    <?php
    //file properties
    //$file= $_FILES['image']['tmp_name'];
    if (!isset($_FILES['image'])) {
    echo "Please select an image.";
    }
else
{
					$image = mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
					$image_name = ($_FILES['image']['name']);
					$namelink = mysql_real_escape_string(htmlentities(strip_tags($_POST['namelink'])));
					$imagelink = mysql_real_escape_string(htmlentities(strip_tags($_POST['imagelink'])));
					$base = mysql_real_escape_string(htmlentities(strip_tags($_POST['base'])));
					$baselink = mysql_real_escape_string(htmlentities(strip_tags($_POST['baselink'])));
					$image_size = getimagesize($_FILES['image']['tmp_name']);
if ($image_size==FALSE){
echo "Thats not an image";
}
else
{
if(!$insert = mysql_query("

					INSERT INTO store VALUES ('','$image_name','$namelink','$image','$imagelink','$base','$baselink')

						"))
						echo"Problem uploading image";
						else
				{
					$lastid = mysql_insert_id();
					echo "Image uploaded.<p />Your image:<p /><img src=getaffiliatestore.php?id=$lastid>";
				}
}

}
?>
<?php include("include/footer.php");?>

hello i have asked this question may i be i did not structure the question well

i am trying to upload(mysql database) and display an image following a tutorial , my image has not displayed, please could someone have a look where i am wrong,
favorite

the two codes i am trying to use are below


 <!--<?php print_r($_FILES);?>-->
    <div>
    <!--<form action='tindex5.php' method='Post' enctype='multipart/form-data'class='rl'>-->
    <form action='affiliatestore.php' method='Post' enctype='multipart/form-data'class='rl'>
        <div>
        <label for='name' class='fixedwidth'>name</label>
        <input type='text' name='name' id='name'/>
        </div>

        <div>
        <label for='namelink' class='fixedwidth'>namelink</label>
        <input type='text' name='namelink' id='namelink'/>
        </div>

        <div>
        <label for='image' class='fixedwidth'>image</label>
        <input type='file' name='image' id='image'/>
        </div>

        <div>
        <label for='imagelink' class='fixedwidth'>imagelink</label>
        <input type='text' name='imagelink' id='imagelink'/>
        </div>

        <div>
        <label for='base' class='fixedwidth'>base</label>
        <input type='text' name='base' id='base'/>
        </div>

        <div>
        <label for='baselink' class='fixedwidth'>baselink</label>
        <input type='text' name='baselink' id='baselink' />
        </div>

        <div class='buttonarea'>
                <p>
                <input type='submit' name='submit' value='submit'>
                </p>
                </div>
    </form>
    </div>
    <?php
    //file properties
    //$file= $_FILES['image']['tmp_name'];
    //if (!isset($_FILES['image'])) {
    if (!isset($_POST['submit'])) {
    echo "Please select an image.";
    }
else
{
                    $image = mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
                    $image_name = ($_FILES['image']['name']);
                    $namelink = mysql_real_escape_string(htmlentities(strip_tags($_POST['namelink'])));
                    $imagelink = mysql_real_escape_string(htmlentities(strip_tags($_POST['imagelink'])));
                    $base = mysql_real_escape_string(htmlentities(strip_tags($_POST['base'])));
                    $baselink = mysql_real_escape_string(htmlentities(strip_tags($_POST['baselink'])));
                    $image_size = getimagesize($_FILES['image']['tmp_name']);
if ($image_size==FALSE){
echo "Thats not an image";
}
else
{
if(!$insert = mysql_query("

                    INSERT INTO store VALUES ('','$image_name','$namelink','$image','$imagelink','$base','$baselink')

                        "))
                        echo"Problem uploading image";
                        else
                {
                    $lastid = mysql_insert_id();
                    //echo $lastid;
                    echo "Image uploaded.<p />Your image:<p /><img src=getaffiliatestore.php?id=$lastid>";
                }
}

}
?>

and the second code affiliatestore.php


$id =  mysql_real_escape_string($_REQUEST['id']);
//echo $id;
$image = mysql_query("SELECT * FROM store WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
$nimage = $image['name'];
//echo $nimage;
header("content-type:image/jpeg");
echo $image;
?>


thanks

i highly advise against storing the image data directly in the database.

It’s possible that while real_escape_string’ing it, you’re corrupting the image data.

Have you checked your database to make sure things are being entered? INSERT…VALUES without field names makes me leery.

the values are being entered in the database including the image, but i have tried everything to make it display but its not .

So, does the image actually upload?
Where do you move it to?
What errors are being shown?

the image uploads in the database,
i try to call the image on the affiliatestore.php
no error is shown

You are storing the image in the db? I have no experience of that, sorry.

I’m out.

thanks for the effort

I do agree with Cups, that storing it in a database is a mistake. However, since that is what you want, what type of field is your image column?
Also try changing

$image = $image['image'];

To

$image = stripslashes($image['image']);

well the fact that you havent put quotes around your src attribute in your image tag might have something to do with it…
beyond that, have you tried commenting out the header line and viewing getaffiliatestore.php directly?

What is the type of your image column? Is it a blob?

yes the image column is blob, i have also commented out the header but same result

:), I think I spotted something

$id =  mysql_real_escape_string($_REQUEST['id']); 
//echo $id; 
$image = mysql_query("SELECT * FROM store WHERE id=$id"); 
$image = mysql_fetch_assoc($image); 
$image = $image['image']; 
$nimage = $image['name']; 
//echo $nimage; 
header("content-type:image/jpeg"); 
echo $image; 
?>  

Take a very close look at your variable names. You are using $image, in all sorts of different meanings, constantly overwriting its value. First it is the result of mysql_query, then the result of mysql_fetch_assoc, then it is the image column value. Try giving it a single purpose, maybe something like so:

$id =  mysql_real_escape_string($_REQUEST['id']); 
//echo $id; 
$imageQuery = mysql_query("SELECT * FROM store WHERE id=$id"); 
$imageResult = mysql_fetch_assoc($imageQuery); 
$image = $imageResult['image']; 
$nimage = $imageResult['name']; 
//echo $nimage; 
header("content-type:image/jpeg"); 
echo $image; 
?>

Also, make sure you have ABSOLUTELY NO WHITESPACE at the end of your PHP file, that will screw up the header output.

i have tried it , still not showing the image