Jump to content

A variable for naming file extension


bytesize

Recommended Posts

This code uploads a file to the server then renames it to the next id number from the database and adds the extension .jpg to the file name.

 

index.php - I need .jpg to be a variable that will append .gif if it's a gif file or .png if it's a png file. Currently, if the file is a .jpg, .gif, or .png, the files are given the extension .jpg.

<?php copy($_FILES['banner']['tmp_name']['file'], './banners/'.$photo_id.'.jpg'); ?>

 

 

_photo.php - I want to replace .jpg in this code with the variable from above.

<a href="<?php echo safe_output($photo['web_url']); ?>" target="_blank" class="bannerImg"><img src="banners/<?php echo $photo['id']; ?>.jpg"/></a>

 

Link to comment
Share on other sites

The problem is the "appropriate extension". Right now .jpg is hard coded in both lines of code. It doesn't matter what file type. I need a variable that includes .jpg, .gif, and .png depending on which file is being uploaded. I'm having trouble creating that variable.

Link to comment
Share on other sites

Store the uploaded files file name within the database, rather than just the id of the photo. That way when you want to display the image you'll use

<a href="<?php echo safe_output($photo['web_url']); ?>" target="_blank" class="bannerImg"><img src="banners/<?php echo $photo['filename']; ?>"/></a>

Link to comment
Share on other sites

The problem is the "appropriate extension". Right now .jpg is hard coded in both lines of code. It doesn't matter what file type. I need a variable that includes .jpg, .gif, and .png depending on which file is being uploaded. I'm having trouble creating that variable.

 

Yes I know that. Thats why you look at the file type and determine what extension to apply.

 

ie.

if($_FILES['banner']['type'] == 'image/jpg')
$ext = '.jpg';
elseif($_FILES['banner']['type'] == 'image/gif')
ext = '.gif';

//etc
//etc

 

wildteen is right. It's better to just store the image name.

 

Link to comment
Share on other sites

I need the id because of the tags connected to each image. How would I puy $ext in place of .jpg in this line of code?

<?php copy($_FILES['banner']['tmp_name']['file'], './banners/'.$photo_id.'.jpg'); ?>

and in here as well?

<a href="<?php echo safe_output($photo['web_url']); ?>" target="_blank" class="bannerImg"><img src="banners/<?php echo $photo['id']; ?>.jpg"/></a>

 

Link to comment
Share on other sites

<?php copy($_FILES['banner']['tmp_name']['file'], './banners/'.$photo_id.$ext); ?>

 

<a href="<?php echo safe_output($photo['web_url']); ?>" target="_blank" class="bannerImg"><img src="banners/<?php echo $photo['id'] . $ext; ?>"/></a>

 

For the second line you'll need to get the image type from getimagesize('image/location'); in order to see what type of ext it is.

 

http://php.net/manual/en/function.getimagesize.php

 

Link to comment
Share on other sites

I can't seem to make this work. If I use $ext it does not add the extension to the file.

 

Where does this code belong?

if($_FILES['banner']['tmp_name']['type'] == 'image/jpg')
  $ext = '.jpg';
  elseif($_FILES['banner']['tmp_name']['type'] == 'image/gif')
  $ext = '.gif';

 

index.php - This is the complete case "create"

<?php
  include('validate.php');
  case "create":
  $page_title = 'Submit Banner';
  $page_on = 'submit';			

  $id_num = mysql_real_escape_string($_POST['banner']['paypal_id']);
  if($id_num > "")
  {
$rs_duplicate = mysql_query("SELECT id FROM photos WHERE paypal_id='$id_num'") or die("MySql Error:<br>".mysql_error());
$duplicate = mysql_num_rows($rs_duplicate);
	   
  if ($duplicate > 0)
  {
  header("Location: index.php");
  exit();
  }
  }	
	  
  if(is_jpeg($_FILES['banner']['type']['file']) 
and is_valid_file_size($_FILES['banner']['size']['file'])
and is_uploaded_file($_FILES['banner']['tmp_name']['file'])
and is_minimum_width_height($_FILES['banner']['tmp_name']['file'])
and is_maximum_width_height($_FILES['banner']['tmp_name']['file'])
   )
  {
  if(is_fields_filled_out($_POST['banner']))
  {
  $_POST['banner']['theme_id'] = $this_weeks_theme['id'];
  $photo_id = create_photo($_POST['banner']);
  copy($_FILES['banner']['tmp_name']['file'], './banners/'.$photo_id.'.jpg');
  /*$notice = "Success! Your banner has been uploaded.";*/
  header("Location: index.php");
  }
  else
  {
$warning = "ERROR: All the text fields must be filled-in.";
  }
  }
  else
  {
$warning = "ERROR: Banner must be a jpg or gif, maximum file size 100k, and not larger than 728px by 90px.";
  }
break;
?>

 

validate.php - This is the file included with index.php

<?php
function is_jpeg($file_type)
{
  if($file_type == 'image/jpeg' or $file_type == 'image/pjpeg' or $file_type == 'image/gif')
  {
return true;
  }
  else
  {
return false;
  }
}

function is_minimum_width_height($minimun)
{
  $min_width = 450;
  $min_height = 50;
   
  list($img_width, $img_height) = getimagesize($minimun);
   
  if($img_width >= $min_width and $img_height >= $min_height)
  {
return true;
  }
  else
  {
return false; 
  }
}

function is_maximum_width_height($maximum)
{
  $min_width = 728;
  $min_height = 90;
   
  list($img_width, $img_height) = getimagesize($maximum);
   
  if($img_width <= $min_width and $img_height <= $min_height)
  {
return true;
  }
  else
  {
return false; 
  }
}

function is_valid_file_size($file_size, $max_size = 100000)
{
  if($file_size > $max_size)
  {
return false;
  }
  else
  {
return true;
  }
}

function is_fields_filled_out($params)
{
  foreach($params as $key => $value)
  {
if(!isset($key) || ($value == ''))
{
  return false;
}
  }		
  return true;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • 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.