Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image Upload Script Problems

Status
Not open for further replies.
Jun 11, 2006
5
0
0
US
Howdy. I've spent the last 3 days, non-stop, trying to put in a simple script to allow users on my site to upload images.

I swear that I've probably used every uploading script out there, but none of them, I've been able to get to work. At first, I was going to do it off of my Freewebs account, but Freewebs doesn't support PHP, which I'm finding out is about the best way to use an uploading script.

So I got an account at FreeWebHostingArea on their ueuo server, which supports PHP (5 I believe) and has a MySQL database. But that completely confuses me.

I know quite a bit about HTML and Javascript, and was hoping that I could just put in the .php in my FileUploader on there, but, I just can't get it to work.

Whenever I try uploading, I just get the 404 error message thing and the image isn't uploading to the directory I assigned it to (which I think may be my problem, because I'm on a Linux server and that's all new to me, and I have everything set to 777).

Would any of you be willing to help me out on this?

Thanks,
Custer
 
Have you tried this one?


It's a PHP class. It's by far my favorite. Not only is it customizable, allows single or multiple files, but it also leaves a blank line of code allowing you to do more with the files when it's done uploading. It even comes with example files. It's really neat. Give it a shot and let us know if you have any questions.
 
Alright, I found one that works. But I would like for it to let the user be able to see the image they just uploaded. How can I put this into the PHP script?
 
Okay, this script works, and when you upload the file, it takes you to the folder where the image was uploaded, but shows all of the images in that folder.

Here's the scripts:
upload1.php
<?php
$num_of_uploads=5;
?>
<form action='/upload2.php' method='post' enctype='multipart/form-data'><input type='hidden' name='submitted' value='TRUE' id='<?=time();?>' >
<input type='hidden' name='MAX_FILE_SIZE' value='<?=$max_file_size;?>' >

<?php for($x=0;$x<$num_of_uploads;$x++){
$form .= "<input type='file' name='file[]'><br>";
}
$form .= "<br><input type='submit' value='Upload File'><br />
<font color='red'>*</font>Maximum file length (minus extension) is 15 characters. Anything over that will be cut to only 15 characters. Valid file type(s): ";
for($x=0;$x<count($file_types_array);$x++){
if($x<count($file_types_array)-1){
$form .= $file_types_array[$x].", ";
}else{
$form .= $file_types_array[$x].".";
}
}
echo($form);
?>

upload2.php
<?php
$file_types_array=array("jpg");
$max_file_size=1048576;
$upload_dir="";

function uploaderFILES($num_of_uploads=1, $file_types_array=array("JPG"), $max_file_size=1048576, $upload_dir="images/"){
if(!is_numeric($max_file_size)){
$max_file_size = 1048576;
}
foreach($_FILES["file"]["error"] as $key => $value)
{
if($_FILES["file"]["name"][$key]!="")
{
if($value==UPLOAD_ERR_OK)
{
$origfilename = $_FILES["file"]["name"][$key];
$filename = explode(".", $_FILES["file"]["name"][$key]);
$filenameext = $filename[count($filename)-1];
unset($filename[count($filename)-1]);
$filename = implode(".", $filename);
$filename = substr($filename, 0, 15).".".$filenameext;
$file_ext_allow = FALSE;
for($x=0;$x<count($file_types_array);$x++){
if($filenameext==$file_types_array[$x])

{
$file_ext_allow = TRUE;

}
} // for
if($file_ext_allow){
if($_FILES["file"]["size"][$key]<$max_file_size){

if(move_uploaded_file($_FILES["file"]["tmp_name"][$key], $upload_dir.$filename)){
echo('<br><br><font color="blue">'.$filename."</font> was successful <br />");
}
else { echo('<br><br><font color="#FF0000">'.$origfilename."</font> was not successfully uploaded <br />");}
}
else { echo('<br><br><font color="#FF0000">'.$origfilename."</font> was too big, not uploaded <br />"); }
} // if
else{ echo('<br><br><font color="#FF0000">'.$origfilename." </font>had an invalid file extension<br />"); }
}
else{ echo('<br><br><font color="#FF0000">'.$origfilename." </font>was not successfully uploaded<br />"); } // else
}
}
} // funtion

/////////////////////////////////////////

if(isset($_POST["submitted"])){
uploaderFILES($num_of_uploads, $file_types_array, $max_file_size, $upload_dir);

}
// Close the FTP connection
ftp_close($conn_id);
?>

<?php
$imgdir = ''; // the directory, where your images are stored
$allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show
$dimg = opendir($imgdir);
while($imgfile = readdir($dimg))
{
if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))
{
$a_img[] = $imgfile;
sort($a_img);
reset ($a_img);
}
}
$totimg = count($a_img); // total image number
for($x=0; $x < $totimg; $x++)
{
$size = getimagesize($imgdir.'/'.$a_img[$x]);
// do whatever
$halfwidth = ceil($size[0]/2);
$halfheight = ceil($size[1]/2);
echo "<img src= '$imgdir/$a_img[$x]'>";
echo 'name: '.$a_img[$x].' width: '.$size[0].' height: '.$size[1].'<br />';
}
?>

So how can I make it so that it only shows the image that was just uploaded only, including it's full url, and, a link to the folder, so the user can still view all of the images in his folder?

Thanks. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top