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
https://forums.phpfreaks.com/topic/92627-image-upload-script-problems/
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";
  }

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.