Jump to content

Upload to show on server delievered page


zeusthegreat

Recommended Posts

form_upload.php

 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'>http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">'>http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

<style type="text/css">

body,td,th {

font-size: 14px;

color: #000;

font-weight: bold;

}

body {

background-color: #0F0;

}

</style>

</head>

 

<body>

<!--next comes the form, you must set the enctype to "multipart/frm-data" and use an input type "file" -->

<form name="newad" method="post" enctype="multipart/form-data" action="upload_image.php">

<table>

<tr><td><input type="file" name="image"></td></tr>

<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>

</table>

</form>

 

 

 

</body>

</html>

 

 

 

 

 

upload_image.php

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

<style type="text/css">

body,td,th {

font-size: 14px;

color: #000;

font-weight: bold;

}

body {

background-color: #0F0;

}

 

img {

background-repeat: no-repeat;

background-position: center center;

width: 100px;

height: 100px;

}

img {

background-repeat: no-repeat;

padding-left: 5px;

}

</style>

</head>

// i have been trying to do what ben told me in my post **uploaded image to show on page** and want to know if i am on the right track

<body>

<p><img src="images/1301661356.jpg" width="1680" height="1050" alt="IMAGE" /><img src="images/1301933656.jpg" width="200" height="200" alt="IMAGE1" /><img src="images/1301581797.jpg" width="1000" height="355" alt="IMAGE2" /><img src="images/1301582412.jpg" width="225" height="338" alt="IMAGE3" /><img src="images/1301586295.jpg" width="200" height="200" alt="IMAGE4" /><img src="images/1301661667.jpg" width="200" height="200" alt="IMAGE5" /><img src="images/1301662173.jpg" width="960" height="960" alt="IMAGE6" /><img src="images/1301662216.gif" width="90" height="90" alt="IMAGE7" /><img src="images/1301662498.jpg" width="200" height="200" alt="IMAGE8" /><img src="images/1301931822.jpg" width="1600" height="1200" alt="IMAGE9" /><img src="images/drupaln.jpg" width="349" height="400" alt="IMAGE10" /><img src="images/5b8c7cf37396b0a35807919bed5788c0.gif" width="160" height="600" alt="IMAGE11" /><img src="images/1974.jpg" width="300" height="481" alt="IMAGE12" /><img src="images/18861.jpg" width="60" height="60" alt="IMAGE13" /><img src="images/486340_vb.jpg" width="200" height="200" alt="IMAGE14" /></p>

<p><img src="images/486346_vb.jpg" width="200" height="200" alt="IMAGE15" /> <img src="images/487214_vb.jpg" width="200" height="200" alt="IMAGE16" /><img src="images/487459_vb.jpg" width="200" height="200" alt="IMAGE17" /></p>

<div>

<?php

//define a maxim size for the uploaded images in Kb

define ("MAX_SIZE","20000000");

 

//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.

function getExtension($str) {

$i = strrpos($str,".");

if (!$i) { return ""; }

$l = strlen($str) - $i;

$ext = substr($str,$i+1,$l);

return $ext;

}

//the above strrpos what does this mean

 

 

//and the exclamation

 

//strlen - what does this mean

//$1 and $i -- what do these mean

 

//substr -- what does this mean

 

// i gather return just returns the extension

 

//This variable is used as a flag. The value is initialized with 0 (meaning no error found) and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded.

$errors=0;

//checks if the form has been submitted-- ****** isset -- what does this mean

if(isset($_POST['Submit']))

{

//reads the name of the file the user submitted for uploading

$image=$_FILES['image']['name'];

//if it is not empty

if ($image)

{

//get the original name of the file from the clients machine **** strtolower what does this mean

$filename = stripslashes($_FILES['image']['name']);

//get the extension of the file in a lower case format

$extension = getExtension($filename);

$extension = strtolower($extension);

//if it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests

if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))

{

//print error message

echo '<h1>Unknown extension!</h1>';

$errors=1;

}

else

{

//get the size of the image in bytes

//$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server

$size=filesize($_FILES['image']['tmp_name']);

 

//compare the size with the maxim size we defined and print error if bigger

if ($size > MAX_SIZE*20000000)

{

echo '<h1>You have exceeded the size limit!</h1>';

$errors=1;

}

 

//we will give an unique name, for example the time in unix time format

$image_name=time().'.'.$extension;

//the new name will be containing the full path where will be stored (images folder)

$newname="images/".$image_name;

//we verify if the image has been uploaded, and print error instead

$copied = copy($_FILES['image']['tmp_name'], $newname);

if (!$copied)

 

{

echo '<h1>Copy unsuccessfull!</h1>';

$errors=1;

}}}}

 

{

// custom area below

PRINT "$image <h1>File Uploaded Successfully! Try again!</h1>";

}

 

?>

 

</div>

</body>

</html>

Link to comment
Share on other sites

To answer a couple of your questions...

 

-- strrpos() returns the position of a substring within a string (http://php.net/manual/en/function.strrpos.php)

-- "!" indicates "not" or "false". So

 

if (!$i) { return ""; }

 

means that if $i is false (the "." wasn't found within the image file name) it will return "". Using a "!" is just another shorter way to write "if ($i == false)".

 

-- isset() checks if a variable has been set. (http://www.php.net/manual/en/function.isset.php)

-- strtolower() converts a string to lower case (http://www.php.net/manual/en/function.strtolower.php)

 

I think the main issue is just a misunderstanding of how the upload process works. You don't upload to the page itself, you upload the image file to a location on the server, and then use PHP to display it by echoing out an <img> tag with the correct src. For example, temporarily replace your upload file with this. After the file has been uploaded, I added a line so that it displays within the page.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
body,td,th {
font-size: 14px;
color: #000;
font-weight: bold;
}
body {
background-color: #0F0;
}

img {
background-repeat: no-repeat;
background-position: center center;
width: 100px;
height: 100px;
}
img {
background-repeat: no-repeat;
padding-left: 5px;
}
</style>
</head>
<body>
<div>
<?php
//define a maxim size for the uploaded images in Kb
define("MAX_SIZE","20000000");

//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
//the above strrpos what does this mean


//and the exclamation

//strlen - what does this mean
//$1 and $i -- what do these mean

//substr -- what does this mean

// i gather return just returns the extension

//This variable is used as a flag. The value is initialized with 0 (meaning no error found) and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded.
$errors=0;
//checks if the form has been submitted-- ****** isset -- what does this mean
if(isset($_POST['Submit']))
{
//reads the name of the file the user submitted for uploading
$image=$_FILES['image']['name'];
//if it is not empty
if ($image)
{
	//get the original name of the file from the clients machine **** strtolower what does this mean
	$filename = stripslashes($_FILES['image']['name']);
	//get the extension of the file in a lower case format
	$extension = getExtension($filename);
	$extension = strtolower($extension);
	//if it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests
	if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
	{
		//print error message
		echo '<h1>Unknown extension!</h1>';
		$errors=1;
	}
	else
	{
		//get the size of the image in bytes
		//$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server
		$size=filesize($_FILES['image']['tmp_name']);

		//compare the size with the maxim size we defined and print error if bigger
		if ($size > MAX_SIZE*20000000)
		{
			echo '<h1>You have exceeded the size limit!</h1>';
			$errors=1;
		}

		//we will give an unique name, for example the time in unix time format
		$image_name=time().'.'.$extension;
		//the new name will be containing the full path where will be stored (images folder)
		$newname="images/".$image_name;
		//we verify if the image has been uploaded, and print error instead
		$copied = copy($_FILES['image']['tmp_name'], $newname);
		if (!$copied)

		{
			echo '<h1>Copy unsuccessfull!</h1>';
			$errors=1;
		}
	}

	/* one line added below ------------------------------------------ */
	echo "<img src='$newname' alt='' /><br/><br/>";
}
}
/* { not necessary */
// custom area below
print "$image <h1>File Uploaded Successfully! Try again!</h1>";
/* } not necessary */
?>

</div>
</body>
</html>

 

If you want an entire directory of images to display, you'd need to create a loop that looks at a the images directory and returns an array of image file names. Something like this may help: http://www.the-art-of-web.com/php/dirlist/1/ or simpler: http://www.phpmagicbook.com/display-images-from-folder/ and http://www.w3schools.com/php/func_directory_dir.asp

Link to comment
Share on other sites

thanks ben

 

how do you say i wanted to show all the images in the images folder at one time on one page

 

say i have 30 images in my images folder and i want them all to show at the same time and when a new image uploaded that is just added to the already 30 images on that page.

 

 

hope this is clear

post-32686-046862600 1302088068_thumb.jpg

Link to comment
Share on other sites

You would want to do something like I mentioned at the end of my last post:

 

If you want an entire directory of images to display, you'd need to create a loop that looks at a the images directory and returns an array of image file names. Something like this may help: http://www.the-art-of-web.com/php/dirlist/1/ or simpler: http://www.phpmagicbook.com/display-images-from-folder/ and http://www.w3schools.com/php/func_directory_dir.asp

Link to comment
Share on other sites

$ret_val or $retval which of these is true and what does it mean

 

implementing the linux route command in php --- $ret_val,

 

i am sorry guys i have to understand what i am doing and what it means or it is though i am half ar$ed and i have never been that....

 

 

 

<?PHP

// Original PHP code by Chirp Internet: www.chirp.com.au

// Please acknowledge use of this code by including this header.

 

function getImages($dir)

{

global $imagetypes;

 

// array to hold return value below is $retval

$retval = array();

 

// add trailing slash if missing

if(substr($dir, -1) != "/") $dir .= "/";

 

// full server path to directory

$fulldir = "{$_SERVER['DOCUMENT_ROOT']}/$dir";

// what does $d mean

//@dir ?

//$entry

$d = @dir($fulldir) or die("getImages: Failed opening directory $dir for reading");

while(false !== ($entry = $d->read())) {

// skip hidden files

if($entry[0] == ".") continue;

 

// check for image files

if(in_array(mime_content_type("$fulldir$entry"), $imagetypes)) {

$retval[] = array(

"file" => "/$dir$entry",

"size" => getimagesize("$fulldir$entry")

);

}

}

$d->close();

 

return $retval;

}

?>

Edited by talos
Link to comment
Share on other sites

$retval, as the comments within the code say, is an array of arrays. At the end of the function, it should either be an empty variable (no image files) or an array that holds the file name and file size of your image files.

 

The $imagetypes variable should be an array that holds the mime types of different file types that you want to display (.jpg, .png, etc. See http://www.webmaster-toolkit.com/mime-types.shtml)

 

$d is a temporary variable used to loop through the files within the folder you provided. It holds a Directory object that helps you with getting the file information.

 

dir() is a function that helps when working with files in a directory. See http://php.net/manual/en/class.dir.php. The "@" symbol in front of the function surpresses any error messages that may occur. See http://www.webmaster-source.com/2011/02/16/php-error-suppression-symbol/ or do a Google search for more info.

 

I'm not seeing any instances of $ret_val in the above code, and I'm not seeing how the linux route command is applicable here? You should be able to use standard PHP for this.

 

Probably the easiest way to see the code you posted in action is to set up a test file that includes the above function. You would need to set up a $imagetypes array, something like:

 

$imagetypes = array("image/jpeg", "image/gif", "image/png");

and call the function/print out the results of the function:

 

$images = getImages("[path to image directory here]");
echo "<pre>";
print_r($images);
echo "</pre>";

Assuming you have everything set up correctly, the array that the getImages function returns should be displayed in the page. This array, which contains all the paths to the file types you specified, can then be used to display your images within the page.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...