Jump to content

Image upload script problems


mistertylersmith

Recommended Posts

PHP version: 5.2.3

WAMP5 Version 1.7.2

MySQL version : 5.0.41-community-nt

 

The problems:

#1: I'm trying to make the script make all the images match this format.. img_1.jpg, img_2.gif, img_3.png ect.. but when the script runs all the images are saved as img_1.gif, img_1.png, img_1.jpg ect.

 

#2: I'm unsure how to make it so only a gif, jpg, or png file can be uploaded

 

#3: I think I need to delete the uploaded image from memory once it has been moved, but I don't know if thats necessary.

 

<?php 
require('functions.php');
session_start();
login_check();

//variables
$user_id = $_SESSION['user_id'];
$file_tmp_name = $_FILES['uploadedfile']['tmp_name'];
$file_name = $_FILES['uploadedfile']['name'];
$file_extension = get_file_extension($file_name);
$image_size = getimagesize($file_tmp_name);
$width1 = $image_size[0];
$height1 = $image_size[1];


//get the last img number
$sql = 'SELECT `img_num` FROM `uploaded_images` ORDER BY `img_num` DESC LIMIT 0, 1';
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
if (mysql_num_rows($result) == 0){
$last_img_num = 0;
}
else if (mysql_num_rows($result) == 1){
$last_img_num = $row['img_num'];
}

$this_img_num = $last_img_num + 1;
$this_img_name = 'img_'.$this_img_num.$file_extension;

//Set the file location
$file_directory = '../pictures/';
$file_location = $file_directory.$this_img_name;
//echo $file_location;
//move_uploaded_file($file_tmp_name, $file_location);

//set thumbnail size
$thumb_x = 0;
$thumb_y = 0;
if ($width1 >= $height1){
$factor = 100 / $width1;
}
else if ($height1 > $width1){
$factor = 100 / $height1;
}
$thumb_x = floor($width1 * $factor);
$thumb_y = floor($height1 * $factor);


//add info to the database
$sql = 'INSERT INTO `uploaded_images` (`img_num`, `img_uploader`, `img_path`, `img_comment`, `img_date`, `img_time`, `img_thumb_x`, `img_thumb_y`, `img_public`, `img_private`) VALUES (NULL, "'.$user_id.'", "'.$file_location.'", NULL, CURDATE(), CURTIME(), "'.$thumb_x.'", "'.$thumb_y.'", "not approved", "visible")';
mysql_query($sql);

session_write_close();
?>

Link to comment
Share on other sites

You haven't specified where you are setting file types that can be uploaded. Right now you are letting everything through.

 

Here is a piece of the code:

 

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
  }
else
  {
  echo "Invalid file";
  }

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.