Jump to content

upload script


sourcecoder

Recommended Posts

Hi i have found this code :

<?php
if (isset($_POST['submit_bilde'])) {
$error = 'Wrong image file..';
define( 'THUMBNAIL_IMAGE_MAX_WIDTH', 250 );
define( 'THUMBNAIL_IMAGE_MAX_HEIGHT', 250 );

function generate_image_thumbnail( $source_image_path, $thumbnail_image_path )
{
  list( $source_image_width, $source_image_height, $source_image_type ) = getimagesize( $source_image_path );

  switch ( $source_image_type )
  {
   case IMAGETYPE_GIF:
    $source_gd_image = imagecreatefromgif( $source_image_path );
    break;

   case IMAGETYPE_JPEG:
    $source_gd_image = imagecreatefromjpeg( $source_image_path );
    break;

   case IMAGETYPE_PNG:
    $source_gd_image = imagecreatefrompng( $source_image_path );
    break;
  }

  if ( $source_gd_image === false )
  {
   return false;
  }

  $thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
  $thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;

  $source_aspect_ratio = $source_image_width / $source_image_height;
  $thumbnail_aspect_ratio = $thumbnail_image_width / $thumbnail_image_height;

  if ( $source_image_width <= $thumbnail_image_width && $source_image_height <= $thumbnail_image_height )
  {
   $thumbnail_image_width = $source_image_width;
   $thumbnail_image_height = $source_image_height;
  }
  elseif ( $thumbnail_aspect_ratio > $source_aspect_ratio )
  {
   $thumbnail_image_width = ( int ) ( $thumbnail_image_height * $source_aspect_ratio );
  }
  else
  {
   $thumbnail_image_height = ( int ) ( $thumbnail_image_width / $source_aspect_ratio );
  }

  $thumbnail_gd_image = imagecreatetruecolor( $thumbnail_image_width, $thumbnail_image_height );

  imagecopyresampled( $thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height );

  imagejpeg( $thumbnail_gd_image, $thumbnail_image_path, 100 );

  imagedestroy( $source_gd_image );

  imagedestroy( $thumbnail_gd_image );

  return true;
}

define( 'UPLOADED_IMAGE_DESTINATION', 'annonsebilder/orginaler/' );
define( 'THUMBNAIL_IMAGE_DESTINATION', 'annonsebilder/thumbs/' );

function process_image_upload( $field )
{
  $temp_image_path = $_FILES[ $field ][ 'tmp_name' ];
  $temp_image_name = $_FILES[ $field ][ 'name' ];

  list( , , $temp_image_type ) = getimagesize( $temp_image_path );

  if ( $temp_image_type === NULL )
  {
   return false;
  }

  switch ( $temp_image_type )
  {

   case IMAGETYPE_JPEG:
    break;

   default:
    return false;
  }

  $uploaded_image_path = UPLOADED_IMAGE_DESTINATION . $temp_image_name;

  move_uploaded_file( $temp_image_path, $uploaded_image_path );
  $random_digit=rand(0000000000000,9999999999999);
  $thumbnail_image_path = THUMBNAIL_IMAGE_DESTINATION . preg_replace( '{\\.[^\\.]+$}', '.jpg', $annonse_ref.'_'.$random_digit.'.jpg' );

  $result = generate_image_thumbnail( $uploaded_image_path, $thumbnail_image_path );

  return $result
   ? array( $uploaded_image_path, $thumbnail_image_path )
   : false;
}

for ( $i = 1; $i <= 5; $i++ )
{
if ( $_FILES[ 'Image' . $i ][ 'error' ] == 0 )
{
  $result = process_image_upload( 'Image' . $i );

}
} 
if ( $result === false )
{
  echo $error;
}
else
{

	//THIS ECHO SHIT IS KILLIN' ME!!!
echo '<br />1: '.$result['1'] ;
echo '<br />2: '.$result['2'];
echo '<br />3: '.$result['3'];
echo '<br />4: '.$result['4'];
echo '<br />5: '.$result['5'];
}

}
if (!isset($_POST['submit_bilde'])) {
  	
   ?>




<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="Image1"><br>
<input type="file" name="Image2"><br>
<input type="file" name="Image3"><br>
<input type="file" name="Image4"><br>
<input type="file" name="Image5"><br><br>
<input type="submit" value="GOOOOOOOO!!!!" name="submit_bilde">
</form>
<?}?>

 

and if you look in the code there is a part where i want to echo the uploaded files' path's..

I only se one of those 5 files listet in the "echo"-thing :P

 

Can someone please, please, please help me to echo ALL the 5 images path??

Link to comment
https://forums.phpfreaks.com/topic/222069-upload-script/
Share on other sites

The echo shit is killing you huh?

 

see your multi upload...

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="Image1"><br>
<input type="file" name="Image2"><br>
<input type="file" name="Image3"><br>
<input type="file" name="Image4"><br>
<input type="file" name="Image5"><br><br>
<input type="submit" value="GOOOOOOOO!!!!" name="submit_bilde">
</form>

 

make it to something like this

<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>">
<input type="file" name="file[]"><br />
<input type="file" name="file[]"><br />
<input type="file" name="file[]"><br />
<input type="file" name="file[]"><br />
<input type="file" name="file[]"><br />
<input type="submit" value="GOOOOOOOO!!!!" name="submit_bilde">
</form>

 

Then you need a loop

//max fle size value
$max_file_size = 50000000;
//timestamp to make files unique names
$timestamp = time();
//added a date for insertion to db
date_default_timezone_set('America/New_York');
$my_date = date('Y-m-d H:i:s');
//destination folder path
$destpath = "storage/";
//looping each file or image from the form
while(list($key,$value) = @each($_FILES["file"]["name"])) {
//check if any empty or errors
if(!empty($value)){
if ($_FILES["file"]["error"][$key] > 0) {
echo "Error: " . $_FILES["file"]["error"][$key] . "<br/>" ;
} else {
//add the timestamp to filename
$file_name = $timestamp.$_FILES['file']['name'];
//temp name from upload form,  key of each is set
$source = $_FILES["file"]["tmp_name"][$key] ;
//getting the file type
$file_type = $_FILES["file"]["type"][$key];
//getting the file size
$file_size = $_FILES["file"]["size"][$key];
//placing each file name into a variable
$filename1 = $_FILES["file"]["name"][$key];
//lowering the file names
$filename = strtolower($filename1);
//adding timestamp to front of file name
$filename = "$timestamp$filename";
//moving the file to be saved from the temp location to the destination path
move_uploaded_file($source, $destpath . $filename);
//the actual final location with timestamped name
$final_file_location = "$destpath$filename";
echo $final_file_location."<br />";
}

 

You can also add to the form titles and description

<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>">
File: <input type="file" name="file[]"/> Title:<input size="40"type="text" name="title[]" style="color: lightblue; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #0000FF;" size="15" value=""> Description:<input size="40"type="text" name="description[]" style="color: lightblue; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #0000FF;" size="15" value=""><br/>
File: <input type="file" name="file[]"/> Title:<input size="40"type="text" name="title[]" style="color: lightblue; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #0000FF;" size="15" value=""> Description:<input size="40"type="text" name="description[]" style="color: lightblue; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #0000FF;" size="15" value=""><br/>
File: <input type="file" name="file[]"/> Title:<input size="40"type="text" name="title[]" style="color: lightblue; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #0000FF;" size="15" value=""> Description:<input size="40"type="text" name="description[]" style="color: lightblue; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #0000FF;" size="15" value=""><br/>
File: <input type="file" name="file[]"/> Title:<input size="40"type="text" name="title[]" style="color: lightblue; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #0000FF;" size="15" value=""> Description:<input size="40"type="text" name="description[]" style="color: lightblue; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #0000FF;" size="15" value=""><br/>
File: <input type="file" name="file[]"/> Title:<input size="40"type="text" name="title[]" style="color: lightblue; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #0000FF;" size="15" value=""> Description:<input size="40"type="text" name="description[]" style="color: lightblue; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #0000FF;" size="15" value=""><br/>
<input type="submit"value="GOOOO!!!!" name="submit_bilde">
</form>

 

In the loop add the values for title or description as each key like so

$final_file_location = "$destpath$filename";
if (file_exists($final_file_location)) {

?>
The file has been uploaded. <a href="<?php echo $final_file_location; ?>" target="_blank"><?php echo $filename; ?></a><br />
<?php
$title = mysql_real_escape_string(trim($_POST["title"][$key]));
if ($title == ''){
$title = mysql_real_escape_string(trim($filename1));
}

$escaped_title = htmlentities($title, ENT_QUOTES);
$description = mysql_real_escape_string(trim($_POST["description"][$key]));
$escaped_description = htmlentities($description, ENT_NOQUOTES);

$display_title = stripslashes($escaped_title);
$display_description = stripslashes($escaped_description);

echo stripslashes($display_title)."<br />";
echo "Type: $file_type<br />";
echo "Size: $file_size<br />";
echo stripslashes($display_description)."<br />";

 

 

For checking for file type

if ($file_type != "image/jpeg" || $file_type != "image/gif" || $file_type != "image/jpg" || $file_type != "image/bmp" || $file_type != "image/png") {
die("	<div align='center' ><h2>You inserted a file type not accepted.</h2><br />
<a href=\"javascript: history.go(-1)\">Click here to try again</a>
</div>");
}

 

Naturally the above I did are not your values and would take some modification of yours.

I been slowly working on a complete upload script with a website and database included.

There are plenty of tutorials on the net for whatever else need to do.

 

 

Link to comment
https://forums.phpfreaks.com/topic/222069-upload-script/#findComment-1149076
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.