Image Upload Script Issues
Posted: Sat Jun 21, 2003 5:46 pm
Hey All! I have been looking around here for some time now and I have found some very useful info here. But, I have run into a snag in my code that I am creating for image uploads on my site. I can get the image to upload fine and that works. However, I cannot get it to check for a valid extention (in this case I want to have only *.jpg files uploaded) and to check the image size to make sure it will fit (I don't know the dimentions yet for this). So, if any of you could help me out that would be great. Also, if you find any errors let me know, I just whipped this up yesterday and am pretty new to upload scripting.
Issues w/ the code below:
1)I cannot get the extention part to work at all... that is why it is commented out.
2)For the image size, it returns Array and I don't know why or how to fix it.
Thanks,
rprins
Issues w/ the code below:
1)I cannot get the extention part to work at all... that is why it is commented out.
2)For the image size, it returns Array and I don't know why or how to fix it.
Thanks,
rprins
Code: Select all
<?php
require('auth.php');
require('../includes/connect.php');
$query = "SELECT * FROM user WHERE userid = '$PHP_AUTH_USER'";
$result = mysql_query($query, $connection) or die("User Id Query Failed.");
while($row = mysql_fetch_array($result)){
$usernumber = $row[usernumber];
$username = $row[username];
$userlevel = $row[userlevel];
$userid = $row[userid];
}
if($action){
switch($action) {
case "upload":
// Display the form for uploading
echo "<form name='upload' method='post' action='upload.php?action=process' enctype='multipart/form-data'>
<p><font>Local Image Dir: </font><input name='source_file' type='file'><br><br>
<input name='process' type='submit' value='Upload Image'>
</form>";
break;
case "process":
// set the login info that is needed
$ftp_server = 'my ftp'; // FTP Server
$ftp_user_name = 'myuser'; // FTP User Login
$ftp_user_pass = 'my pass'; // FTP User Password
// set up basic connection
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!<br>";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
}
/*
// Limit uploads to specific extentions -- THIS DOES NOT WORK! NEED HELP HERE
$limitedext = array(".gif",".jpg",".png",".jpeg");
$ext = strrchr($file_name,'.');
if (!in_array($ext,$limitedext)) {
die("The file you are uploading doesn't have the correct extension.");
exit;
}
*/
$filesize = filesize($source_file);
if($filesize > 40960){
echo "Your filesize, " . $filesize . " bytes, is too large.<br>
It must be less than 40KB or 40960 bytes.";
exit;
}
$file_extention = '.jpg'; // Define the file extention. MUST include the period before the extention
$destination_path = '/www/images/roster/'; // Define path to the file on the server
$destination_file = $destination_path . $usernumber . $file_extention; // Define the file name
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_ASCII); // upload the file
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file<br>";
}
// Check image size -- THIS DOES NOT WORK! NEED HELP HERE
$image = "../images/roster/" . $usernumber . ".jpg";
$size = getimagesize ($image);
echo "<img src="". $image . "" {$size[3]}><br>";
echo $image . $size . "<br>";
$delete_temp = unlink($source_file); // Delete file in Temp Dir
if($delete_temp) echo "<br>Success Deleting Temp Image";
// close the FTP stream
ftp_close($conn_id);
break;
}
}
?>