I'm having a lot of problems doing a kind of complicated upload script. What it should do is rename the file to, for example, comic.jpg and based if comic.jpg exists already it should re-rename it into comic_2.jpg or something.
The radio buttons I'm only writing so I don't forget what I want the script to eventually do in the future, but it's commented out so it shouldn't matter.
Right now the code isn't working as it is. I'm trying to figure out way. It won't even upload correctly. It just says "file exists" and ignores to add the _2. It won't even rename the file. I'm just totally lost 🙁
Can anyone give me a hand? or point me in the direction of a finished script? or tell me what I'm doing wrong?
<FORM ENCTYPE="multipart/form-data" ACTION="<?php $self; ?>" METHOD="POST">
The file: <INPUT TYPE="file" NAME="userfile"><br>
<?php
/*
<input type="radio" value="picture" name="Comic">Picture<br>
<input type="radio" value="comic" name="comic">Comic<br>
*/
?>
<INPUT TYPE="submit" VALUE="Upload">
</FORM>
<?php
$path = "/home/john/public_html/comic2/";
$max_size = 200000;
/*
$comicpost = $_POST['cominc'];
$picturepost = $_POST['picture'];
if (isset($_POST['celda'])) {
$celda = "celda";
}
if (isset($_POST['comic'])) {
// rename($HTTP_POST_FILES['userfile']['name'],'comic');
}
*/
if (!isset($HTTP_POST_FILES['userfile'])) exit;
if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {
if ($HTTP_POST_FILES['userfile']['size']>$max_size) { echo "The file is too big<br>\n"; exit; }
if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg")) {
$i = 0;
//if the filename already exists, append _copy_x (with extension)
if(strpos($HTTP_POST_FILES['userfile']['name'],'.') !== false){
$bits = explode('.',$HTTP_POST_FILES['userfile']['name']);
$ext = array_pop($bits);
while(file_exists($destDir.implode('.', $bits).($i?'_copy_'.$i:'').'.'.$ext)){
++$i;
$HTTP_POST_FILES['userfile']['name'] = implode('.',$bits).($i?'_copy_'.$i:'').'.'.$ext;
}
if (file_exists($path . $HTTP_POST_FILES['userfile']['name'])) { echo "The file already exists<br>\n"; exit; }
$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$HTTP_POST_FILES['userfile']['name']);
}
if (!$res) { echo "upload failed!<br>\n"; exit; } else { echo "upload sucessful<br>\n"; }
echo "File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>\n";
echo "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>\n";
echo "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>\n";
} else { echo "Wrong file type<br>\n"; exit; }
}
?>
Thanks for reading/replying 🙂!