Help With Upload Script I'm Writing
06-26-2006, 08:11 PM
|
Help With Upload Script I'm Writing
|
Posts: 7
Name: Frederick
|
I've been writing a script that will allow users to upload images to my site, and it was working perfectly, but I had to change a directory problem, and now, it won't let me upload anything. Can somebody please help me?
upload2.php
PHP Code:
<?php $file_types_array=array("jpg"); $max_file_size=1048576; $upload_dir="../guild"; function uploaderFILES($num_of_uploads=1, $file_types_array=array("JPG"), $max_file_size=1048576, $upload_dir="../guild"){ 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 = '../guild'; // 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 'link: http://eye-candy-guild.ueuo.com/guild/'.$a_img[$x].' width: '.$size[0].' height: '.$size[1].'<br />'; } ?>
|
|
|
|
06-26-2006, 08:18 PM
|
Re: Help With Upload Script I'm Writing
|
Posts: 2,111
Name: Matt. (>',')>
Location: London, England.
|
What happens when you try to upload? Any errors?
Have you created the directory it's tryin to upload to?
Does that directory have enough permissions to allow it to be written to?
|
|
|
|
06-26-2006, 08:24 PM
|
Re: Help With Upload Script I'm Writing
|
Posts: 7
Name: Frederick
|
The way I had it before, it was uploading images, but to the wrong directory, so I figured out that I had to put in"../directoryname" for the directory so it would upload to the correct directory and not just the main directory where everyone could see it. Let me touch on that I'm making a directory for all of the members on my site, but before I fixed the directory thing, they were all going to the same folder and when it uploaded, it viewed all of the images from that folder. So I got it working, and I had to play with the echos a little, but when I changed those back, I keep getting an error message saying, "'original name of file' was not successfully uploaded."
What's wrong?
|
|
|
|
06-26-2006, 08:28 PM
|
Re: Help With Upload Script I'm Writing
|
Posts: 2,111
Name: Matt. (>',')>
Location: London, England.
|
if you are trying to upload to a directory that is in the directory that contains the script try changing ../guild to guild
|
|
|
|
06-26-2006, 08:31 PM
|
Re: Help With Upload Script I'm Writing
|
Posts: 7
Name: Frederick
|
I'm for sure that it has to be "../guild" because that's the only one that will upload to the correct directory. But I think my problem now is in the problem echos, or somewhere like that...
|
|
|
|
06-26-2006, 08:43 PM
|
Re: Help With Upload Script I'm Writing
|
Posts: 7
Name: Frederick
|
Would anyone be willing to help me rewrite this script or at least get it working.
If you want to try the script, go here: http://eye-candy-guild.ueuo.com/guild/upload1.php
But like I said before, it's not working correctly... 
|
|
|
|
06-28-2006, 05:54 AM
|
Re: Help With Upload Script I'm Writing
|
Posts: 880
Location: Leeds UK
|
Make sure your image dir has the correct permision. a+rw should be enough but if your on NIX simply chgrp apache /path/to/images
PHP Code:
<?php
class image{
public $ImgFrom;
public $ImgTo;
public $path;
public function __construct(){ }
public function setFile($from, $to){
$this->ImgFrom=$from;
$this->ImgTo=$to;
}
public function setPath($path){
$this->path=$path;
}
public function save(){
if (move_uploaded_file($this->ImgFrom, $this->path.$this->ImgTo)) {
return true;
}
return false;
}
public function delete($path,$img){
if(unlink($path . $img)){
return true;
}
return false;
}
}
// the above class itself should be self explanitory
if(isset($_FILES['uploadimage1'])){
// we have a file
include('classes/image_class.php');
$img = new image();
// Make sure we know our image ID or name or what you naming convention will be
if(!empty($this->postvars['imgId'])){
// set path and image from=>to
$img->path = "/home/webuser/public_html/images/comp/"; // set image upload dir
$img->setFile($_FILES['uploadimage1']['tmp_name'], $this->postvars['imgId'] .".jpg"); // img from=>to
// save it
if(!empty($img->ImgFrom))
if(!$img->save()) var_dump($this->postvars['imgId'] .".jpg save failed!"); // save it
?>
The issue with file uplods is and always will be the permisions of the dir your looking at getting them uploaded to. Of course your form should also have a hidden field like this:
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
Which allows you to upload files in the first place. I think you will find if this is missing that it will be a reason too for files not uploading.
Let us know how you get on
Ibbo
|
|
|
|
06-29-2006, 12:20 AM
|
Re: Help With Upload Script I'm Writing
|
Posts: 7
Name: Frederick
|
Alright, last night, I took a look at it, and thanks to some help from a friend, we were able to fix it, for the most part. Problem was, the file types that were being arrayed were being arrayed throughout the entire script, if that makes sense, so here's the new script:
upload2.php
PHP Code:
<?php function uploaderFILES($num_of_uploads=1, $file_types_array=array("JPG"), $max_file_size=1048576, $upload_dir="../guild"){ 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; $file_types_array=array("jpg", "JPG", "jpeg", "JPEG"); 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 = '../guild'; // 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 'link: http://eye-candy-guild.ueuo.com/guild/'.$a_img[$x].' width: '.$size[0].' height: '.$size[1].'<br />'; } ?>
Now the problem is, whenever you upload the file, it brings you to the output screen, and that says you uploaded your file successfully, but it displays all of the images in the directory you assigned it to. I only want it to show the image that you just uploaded, but provide a link so you can see all of the images in the directory.
I'm for sure that the second script in this script (after the close ftp connection) deals with the output screen, in terms of other things being shown, so that's what's going to be edited. But, I'm stumped onto what I should edit. Any ideas?
Thanks. 
|
|
|
|
07-01-2006, 09:29 PM
|
Re: Help With Upload Script I'm Writing
|
Posts: 7
Name: Frederick
|
Anyone?
|
|
|
|
|
« Reply to Help With Upload Script I'm Writing
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|