I’ve search a pretty good deal on the forum already, and couldn’t implement the examples I have found. I was hoping someone could assist me in adding some functionality to this movie upload script.
Currently if a duplicate filename exists it outputs an error, and asks the user to rename their file… this is frustrating to the users because they are forced to re-upload (wait for) sometimes up to 15MB files (soon to be 50mb).
So I’d like to add a section of code to check if a duplicate filename exists, and if it does in fact, then rename the file with an incrementing extensiopn like copy1, copy2, etc. before copying it to the upload directory
Would someone mind helping, please ? Thank you very very much in advance.
Here’s my script:
<?
switch($upload) {
default:
include "edit_upload_config.php";
echo "
<div align=\\"center\\">
<center>
<table border=\\"0\\" cellpadding=\\"0\\" cellspacing=\\"0\\" style=\\"border-collapse: collapse\\" bordercolor=\\"#111111\\" width=\\"400\\" id=\\"AutoNumber1\\">
<tr>
<td class=\\"smallboldred\\">
<img src=\\"images/content_heads/edit_hosting_head.gif\\"><br><br>
[ READ THESE RESTRICTIONS BEFORE UPLOADING! ]<br><br>
<div class=\\"smallbold\\">
<img src=images/blink_bullet.gif> File extension must be <b> .mov .avi .asf .mpg .mpg4 .wmv .mpeg .mp4<br>";
/* BEGIN COMMENT FOR EXTENSION ECHOING if (($extensions == "") or ($extensions == " ") or ($ext_count == "0") or ($ext_count == "") or ($limit_ext != "yes") or ($limit_ext == "")) {
echo "any extension";
} else {
$ext_count2 = $ext_count+1;
for($counter=0; $counter<$ext_count; $counter++) {
echo " $extensions[$counter]";
}
} END COMMENT FOR EXTENSION ECHOING */
if (($limit_size == "") or ($size_limit != "yes")) {
$limit_size = "any size";
} else {
$limit_size .= " bytes";
}
echo"</b></li>
<img src=images/blink_bullet.gif> Maximum file size is $limit_size (<b>50 MB</b>) (<b>51200 KB</b>)<br>
<img src=images/blink_bullet.gif> Filename cannot contain illegal characters
(/,*,\\,etc)<br>
</div>
<form method=\\"POST\\" action=\\"$PHP_SELF?upload=doupload\\" enctype=\\"multipart/form-data\\" onSubmit=\\"submitonce(this)\\">
<p align=\\"center\\">
<input type=file name=file class=\\"shadebox\\"><br>
<br>
<INPUT type=submit value=\\"Upload\\" class=\\"selectall\\">
</p>
<table width=\\"400\\" border=\\"0\\" cellpadding=\\"8\\" cellspacing=\\"0\\" class=\\"mainBord\\" background=\\"images/notice_bg.gif\\">
<tr>
<td class=\\"smallboldred\\" align=\\"center\\">
<img src=\\"images/uhoh.gif\\"> <b>[ NOTICE! ]</b> <img src=\\"images/uhoh.gif\\"><br>
<div class=\\"smallbold\\" align=\\"left\\">
Uploading your edit file is not an instant process. It may take any given amount of time depending on the size of your edit file, and your connection speed to the internet. So please be patient before reporting an error. The larger your file, the longer it will take!
</div>
</td>
</tr>
</table>
</form>
<p>
</td>
</tr>
</table>
</center>
</div>";
break;
case "doupload":
include "edit_upload_config.php";
$file_name = stripslashes($file_name);
$to_replace= array(" ", "'", "#");
$replace_with = array("_", "_", "_");
$file_name = str_replace($to_replace, $replace_with, $file_name);
$endresult = "<table border=0 cellpadding=0 cellspacing=0 width=402><tr><td class=smallboldred><div align=center><b>SUCCESS! - File Was Uploaded</div></b><p><div align=\\"left\\" class=\\"smallboldred\\"><img src=images/blink_bullet.gif> Here's how to copy your link into the forum:</div>
<div class=\\"small\\" align=\\"left\\">
<b><font color=\\"#000000\\">Step 1</font></b>: Press the \\"Select All\\" button and then \\"CTRL + C\\" (\\"command + C\\" on an Apple) on your keyboard to copy the code. Then proceed to Step 2.<br>
<b><font color=\\"#000000\\">Step 2</font></b>: Paste the link with \\"CTRL + V\\" (\\"command + V\\" on an Apple) into your forum post to link to your edit .<br>
</div>
<form name=\\"copy\\" method=\\"none\\">
<textarea name=txt class=\\"uploadtextarea\\">
[http://www.mysite.com/edit_uploads/$file_name</textarea](http://www.mysite.com/edit_uploads/$file_name</textarea)>
<br>
<INPUT type=button value=\\"Select All\\" class=\\"selectall\\" onClick=\\"javascript:this.form.txt.focus();this.form.txt.select();\\">
</form>
<p>
<div align=\\"left\\"><img src=images/blink_bullet.gif> <a href=\\"[http://www.mysite.com/forum\\](http://www.mysite.com/forum/)" target=\\"_self\\">Go to the Forum</a>
<br>
<img src=images/blink_bullet.gif> <a href=\\"[http://www.mysite.com/edit_uploads.php\\](http://www.mysite.com/edit_uploads.php/)" target=\\"_self\\">Upload Another Edit</a>
<br>
<img src=images/blink_bullet.gif> <a href=\\"[http://www.mysite.com/pics\\](http://www.mysite.com/pics/)" target=\\"_self\\">Upload a Pic</a>
</div></td></tr></table>";
if ($file_name == "") {
$endresult = "<img src=\\"images/uhoh.gif\\"> <b>No file was selected - please use the browse button at the beginning to choose your file.</b><br><a href=\\"edit_uploads.php\\">Try Again?</a>";
}else{
if(file_exists("$absolute_path/$file_name")) {
$endresult = "<img src=\\"images/uhoh.gif\\"> <b>A file with that name already existed - rename your file.</b> <br><a href=\\"edit_uploads.php\\">Try Again?</a>";
} else {
if (($size_limit == "yes") && ($limit_size < $file_size)) {
$endresult = "<img src=\\"images/uhoh.gif\\"> <b>The file size is over the limit - accepted sizes are listed at the beginning.</b><br><a href=\\"edit_uploads.php\\">Try Again?</a>";
} else {
$ext = strrchr($file_name,'.');
if (($limit_ext == "yes") && (!in_array($ext,$extensions))) {
$endresult = "<img src=\\"images/uhoh.gif\\"> <b>The file type is wrong - the accepted types are listed at the beginning.</b><br><a href=\\"edit_uploads.php\\">Try Again?</a>";
}else{
$file_name = stripslashes($file_name);
$to_replace= array(" ", "'", "#");
$replace_with = array("_", "_", "_");
$file_name = str_replace($to_replace, $replace_with, $file_name);
@copy($file, "$absolute_path/$file_name") or $endresult = "<img src=\\"images/uhoh.gif\\"> <b>There's been a error, the file could not be copied.</b><br><a href=\\"edit_uploads.php\\">Try Again?</a>";
}
}
}
}
echo "
<div align=\\"center\\">
<center>
<table border=\\"0\\" cellpadding=\\"0\\" cellspacing=\\"0\\" id=\\"AutoNumber1\\">
<tr>
<td class=\\"small\\">
<center> $endresult </center>
</td>
</tr>
</table>
</center>
</div>
";
break;
}
?>
[fphp]file_exists[/fphp] can be used to check if the file is already there. If it is just take the name from the upload and if your using a naming convention you can do a regex on it and get the number portion off of it, increment it and concat it back on. I’m not going to try to sort through the code you posted. Hopefully this suggestion will get you in the right direction or some one else will also help.
Thank you, I already found some examples like that when searching the forum, but I guess the problem is if I had a small snippet of code to rename my $file_name variable if it aready exists, I don’t know where to add it into my code.
I implemented my rename script, and now I am getting this permission denied error, help please - why can’t I rename the file !?
This sure is getting tough.
[b]
[b]Warning: rename(/****/*****/*****/clipoftheweek3.wmv,965clipoftheweek3.wmv): Permission denied in /****/*****/*****/edit_uploads.php on line 11[/b]
[/b]
[b]Here’s the script I am using now (which falls on line 115):
One small problem - I’d like to have this textarea echo the link to the uploaded file when it’s completed, and only if it’s successfully completed. I’ve tried it all over the script, but can’t get it to echo the filename, I’m not sure where to put this bit of code…
Any suggestions ?
Thanks
Is there some sort of if statement I could write to check if it was all successfully completed, and only echo this is it was ?
I’ve used is if fileexists, and it’s not working for me.
I have a simpler question then, if I want to echo the file path only upon successful upload (i.e. no errors are found in my error checking if statements) what would the scrip tbe to to do that, and where in the code would it go ?
Actually, lemme try a different approach. Here is the script exactly as it stands right now. I only want $endresult to return the textarea if none of the other $endresults contain errors… so do I need to write in another condition, and if so what is it & where do I put it in the script? hopefully this makes sense. I’m pulling my hair out right now.
Here’s the way it needs to work:
User Uploads file, if user meets all conditions (proper extension, size, etc), then file is uploaded/renamed if necessary, and the textarea is displayed with their file link. If not, they are displayed the error that resulted.
Thank you thank you thank you to someone who can lift me up !
<?php
include 'edit_upload_config.php';
switch ($upload) {
case 'doupload':
$file_name = stripslashes($file_name);
$to_replace = array(' ', '\\'', '#');
$replace_with = array('_', '_', '_');
$file_name = str_replace($to_replace, $replace_with, $file_name);
$endresult = '<table border="0" cellpadding="0" cellspacing="0" width="402">
<tr>
<td class="smallboldred">
<div align="center">
<b>success! - file was uploaded</b>
</div></b>
<div align="left" class="smallboldred">
<img src="images/blink_bullet.gif"> here\\'s how to copy your link into the forum:
</div>
<div class="small" align="left">
<b><font color="#000000">step 1</font></b>: press the "select all" button and then "CTRL + C" ("command + c" on an apple) on your keyboard to copy the code. then proceed to step 2.<br>
<b><font color="#000000">step 2</font></b>: paste the link with "ctrl + v" ("command + v\\" on an apple) into your forum post to link to your edit.<br>
</div>
<form name="copy" method="none">
<textarea name="txt" class="uploadtextarea">http://www.mysite.com/edit_uploads/'.$new_name.'</textarea><br>
<input type="button" value="select all" class="selectall" onclick="java_script_:this.form.txt.focus();this.form.txt.select();">
</form>
<p>
<div align="left">
<img src="images/blink_bullet.gif"> <a href="[http://www.mysite.com/forum/](http://www.mysite.com/forum/)" target="_self">go to the forum</a>
<br>
<img src=images/blink_bullet.gif> <a href="[http://www.mysite.com/edit_uploads.php](http://www.mysite.com/edit_uploads.php)" target="_self">upload another edit</a>
<br>
<img src=images/blink_bullet.gif> <a href="[http://www.mysite.com/pics/](http://www.mysite.com/pics/)" target="_self">upload a pic</a>
</div>
</p>
</td>
</tr>
</table>';
if ($file_name == '') {
$endresult = '<img src="images/uhoh.gif">
<b>No file was selected - please use the browse button at the beginning to choose your file.</b>
<br><a href="edit_uploads.php">Try Again?</a>';
} elseif (($size_limit == 'yes') && ($limit_size < $file_size)) {
$endresult = '<img src="images/uhoh.gif">
<b>The file size is over the limit - accepted sizes are listed at the beginning.</b>
<br><a href="edit_uploads.php">Try Again?</a>';
} else {
$ext = strrchr($file_name, '.');
if (($limit_ext == 'yes') && (!in_array($ext, $extensions))) {
$endresult = '<img src="images/uhoh.gif">
<b>The file type is wrong - the accepted types are listed at the beginning.</b>
<br><a href="edit_uploads.php">Try Again?</a>';
} else {
$new_name = $file_name;
if (file_exists($absolute_path.'/'.$file_name)) {
$i = 1;
$file_ext = substr($file_name, strrpos($file_name, '.'));
$file_part_name = substr($file_name, 0, strrpos($file_name, '.'));
do {
$new_name = $file_part_name . '_' . $i++ . $file_ext;
} while (file_exists($absolute_path.'/'.$new_name));
}
move_uploaded_file($file, $absolute_path.'/'.$new_name)
or $endresult = '<img src="images/uhoh.gif">
<b>There\\'s been a error, the file could not be copied.</b>
<br><a href="edit_uploads.php\\>Try Again?</a>';
}
}
echo '
<div align="center">
<center>
<table border="0" cellpadding="0" cellspacing="0" id="AutoNumber1">
<tr>
<td class="small">
<center>'.$endresult.'</center>
</td>
</tr>
</table>
</center>
</div>';
break;
default:
?>
<div align="center">
<center>
<table border="0\\" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="400" id="AutoNumber1">
<tr>
<td class="smallboldred">
<img src="images/content_heads/edit_hosting_head.gif"><br><br>
[ READ THESE RESTRICTIONS BEFORE UPLOADING! ]<br><br>
<div class="smallbold">
<img src="images/blink_bullet.gif"> File extension must be <b> .mov .avi .asf .mpg .mpg4 .wmv .mpeg .mp4</b><br>
<?php
if ($limit_size == '' or $size_limit != 'yes') {
$limit_size = 'any size';
} else {
$limit_size .= ' bytes';
}
?>
<img src="images/blink_bullet.gif"> Maximum file size is <?php echo $limit_size; ?>.' (<b><?php echo ceil($limit_size/1024/1024); ?> MB</b>) (<b><?php echo ceil($limit_size/1024); ?> KB</b>)<br>
<img src="images/blink_bullet.gif"> Filename cannot contain illegal characters (/, *, \\, etc)<br>
</div>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?upload=doupload" enctype="multipart/form-data" onSubmit="submitonce(this)">
<p align="center">
<input type="file" name="file" class="shadebox"><br>
<br>
<input type="submit" value="upload" class="selectall">
</p>
<table width="400" border="0" cellpadding="8" cellspacing="0" class="mainBord" background="images/notice_bg.gif">
<tr>
<td class="smallboldred" align="center">
<img src="images/uhoh.gif"> <b>[ NOTICE! ]</b> <img src="images/uhoh.gif"><br>
<div class="smallbold" align="left">
Uploading your edit file is not an instant process. It may take any given amount of time depending on the size of your edit file, and your connection speed to the internet. So please be patient before reporting an error. The larger your file, the longer it will take!
</div>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</center>
</div>
<?php
}
?>