Go Back   Free website templates > Web Template Help > PHP and MySQL
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Join now to download all the free website templates or post on the forum. If you have never been on a forum before read the FAQ. It's quick, easy and free to join!
Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-24-2008, 12:54 AM
Jaybirddesigns's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Helendale,ca
Posts: 133
Jaybirddesigns is on a distinguished road
Default File Upload Replace

I have a script that uploads fine, but i want it to replace the curent file in that specific Dir. Not however make a temp name. So basiclly if i upload pic1.jpg it will replace pic1.jpg, not make it 000123pic1.jpg

Any Ideas?

Script

PHP Code:
<?php 
session_start
(); 
if(!isset(
$_POST['upload'])) { 
echo 

<form name="upload" enctype="multipart/form-data" method="POST" action="'
.$_SERVER['REQUEST_URI'].'"> 
<input type="file" name="file" size="13" value=""> 
<br /><input type="submit" name="upload" value="Upload"> 
</form> 
'

} else { 
$yourdomain 'http://www.jaybirdfolio.com/'
$uploaddir 'images/'
$filename $_FILES['file']['name']; 
$filesize $_FILES['file']['size']; 
if(
$filesize '5000000') { 
echo 
"Way too big!!"
} else { 
move_uploaded_file("$uploaddir$filename"); 
echo 
"Successful.<br /><b>URL: </b><textarea rows='1' cols='13'>".$yourdomain.$uploaddir.$date_file.$filename."</textarea>"

  } 
?>
Reply With Quote
  #2 (permalink)  
Old 05-24-2008, 01:11 AM
Jaybirddesigns's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Helendale,ca
Posts: 133
Jaybirddesigns is on a distinguished road
Default

Nevermind Ubove code no worky
Reply With Quote
  #3 (permalink)  
Old 05-24-2008, 07:00 AM
pezboy45's Avatar
Mod/Design & Coder [Pro]
 
Join Date: Nov 2006
Location: Hawaii
Posts: 686
pezboy45 is on a distinguished road
Send a message via AIM to pezboy45 Send a message via MSN to pezboy45
Default

I actually wrote this script for images, but you can edit it for any files.
(just change the extensions!)

I also got rid of all the syleing (I built it for a img script hosting site, view here)

But, here's the script:
Be sure to name the file: index.php
PHP Code:
<?
error_reporting
(7);
// Max size PER picture in KB, not bytes for simplicity!
$max_file_size="5120";

// Max size for all picutres COMBINED in KB, not bytes for simplicity!
$max_combined_size="25600";

//How many file uploads do you want to allow at a time?
$file_uploads="5";

//The name of the uploader..
$websitename="Your name here";

// Use random file names? true=yes (recommended), false=use original file name. Random names will help prevent overwritting of existing files!
$random_nametrue;

// Please keep the array structure.
$allow_types=array("jpg""jpeg""gif""png");

// Path to files folder. If this fails use $fullpath below. With trailing slash
$folder="path/to/images/";

// Full url to where files are stored. With Trailing Slash
$full_url="http://yoururl.com/path/to/images/";

// Only use this variable if you wish to use full server paths. Otherwise leave this empty! With trailing slash
$fullpath="/http/yoururl.com/full/path/to/images/";

//Use this only if you want to password protect your uploads.
//Not recommended for free picture hosting sites)
$password=""

/*
//================================================================================
* ! ATTENTION !
//================================================================================
: Don't edit below this line unless you know some php. Editing some variables or other stuff could cause undeseriable results!!
*/

// MD5 the password.. why not?
$password_md5=md5($password);

// If you set a password this is how they get verified!
If($password) {
    If(
$_POST['verify_password']==true) {
        If(
md5($_POST['check_password'])==$password_md5) {
            
setcookie("phUploader",$password_md5,time()+86400);
            
sleep(1); //seems to help some people.
            
header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
            exit;
            
        }
    }
}

// The password form, if you set a password and the user has not entered it this will show.
$password_form="";
If(
$password) {
    If(
$_COOKIE['phUploader']!=$password_md5) {
        
$password_form="<form method=\"POST\" action=\"".$_SERVER['PHP_SELF']."\">\n";
        
$password_form.="<table align=\"center\" class=\"table\">\n";
        
$password_form.="<tr>\n";
        
$password_form.="<td width=\"100%\" class=\"table_header\" colspan=\"2\">Password Required</td>\n";
        
$password_form.="</tr>\n";
        
$password_form.="<tr>\n";
        
$password_form.="<td width=\"35%\" class=\"table_body\">Enter Password:</td>\n";
        
$password_form.="<td width=\"65%\" class=\"table_body\"><input type=\"password\" name=\"check_password\" /></td>\n";
        
$password_form.="</tr>\n";
        
$password_form.="<td colspan=\"2\" align=\"center\" class=\"table_body\">\n";
        
$password_form.="<input type=\"hidden\" name=\"verify_password\" value=\"true\">\n";
        
$password_form.="<input type=\"submit\" value=\" Verify Password \" />\n";
        
$password_form.="</td>\n";
        
$password_form.="</tr>\n";
        
$password_form.="</table>\n";
        
$password_form.="</form>\n";
    }
}

// Function to get the extension a file.
function get_ext($key) { 
    
$key=strtolower(substr(strrchr($key"."), 1));
    
// Cause there the same right?
    
$key=str_replace("jpeg","jpg",$key);
    return 
$key;
}

$ext_count=count($allow_types);
$i=0;
foreach(
$allow_types AS $extension) {
    
    
//Gets rid of the last comma for display purpose..
    
    
If($i <= $ext_count-2) {
        
$types .="*.".$extension.", ";
    } Else {
        
$types .="*.".$extension;
    }
    
$i++;
}
unset(
$i,$ext_count); // why not

$error="";
$display_message="";
$uploaded==false;

// Dont allow post if $password_form has been populated
If($_POST['submit']==true AND !$password_form) {

    For(
$i=0$i <= $file_uploads-1$i++) {
                    
        If(
$_FILES['file']['name'][$i]) {
                        
            
$ext=get_ext($_FILES['file']['name'][$i]);
            
$size=$_FILES['file']['size'][$i];
            
$max_bytes=$max_file_size*1024;
            
            
// For random names
            
If($random_name){
                
$file_name[$i]=time()+rand(0,100000).".".$ext;
            } Else {
                
$file_name[$i]=$_FILES['file']['name'][$i];
            }
            
            
//Check if the file type uploaded is a valid file type. 
                        
            
If(!in_array($ext$allow_types)) {
                            
                
$error.= "Invalid extension for your file: ".$_FILES['file']['name'][$i].", only ".$types." are allowed.<br />Your file(s) were <b>not</b> uploaded.<br />";
                            
                
//Check the size of each file
                            
            
} Elseif($size $max_bytes) {
                
                
$error.= "Your file: ".$_FILES['file']['name'][$i]." is to big. Max file size is ".$max_file_size."kb.<br />Your file(s) were <b>not</b> uploaded.<br />";
                
                
// Check if the file already exists on the server..
            
} Elseif(file_exists($folder.$file_name[$i])) {
                
                
$error.= "The file: ".$_FILES['file']['name'][$i]." exists on this server, please rename your file.<br />Your file(s) were <b>not</b> uploaded.<br />";
                
            }
                        
        } 
// If Files
    
    
// For
    
    //Tally the size of all the files uploaded, check if it's over the ammount.
                
    
$total_size=array_sum($_FILES['file']['size']);
                  
    
$max_combined_bytes=$max_combined_size*1024;
                
    If(
$total_size $max_combined_bytes) {
        
$error.="The max size allowed for all your files combined is ".$max_combined_size."kb<br />";
    }
        
    
    
// If there was an error take notes here!
    
    
If($error) {
        
        
$display_message=$error;
        
    } Else {
        
        
// No errors so lets do some uploading!
        
        
For($i=0$i <= $file_uploads-1$i++) {
                
            If(
$_FILES['file']['name'][$i]) {
                
                If(@
move_uploaded_file($_FILES['file']['tmp_name'][$i],$folder.$file_name[$i])) {
                    
$uploaded=true;
                } Else {
                    
$display_message.="Couldn't copy ".$file_name[$i]." to server, please make sure ".$folder." is chmod 777 and the path is correct.\n";
                }
            }
                
        } 
//For
        
    
// Else
    
// $_POST AND !$password_form

/*
//================================================================================
* Start the form layout
//================================================================================
:- Please know what your doing before editing below. Sorry for the stop and start php.. people requested that I use only html for the form..
*/
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo $websitename?>::Uploader</title>
<?
If($password_form) {
    
    Echo 
$password_form;
    
} Elseif(
$uploaded==true) {?>
<body>
<table align="center"class="table">

    <tr>
        <td class="table_header" colspan="2"><div align="center">
          <h1><b>your file(s) have been uploaded!</b> </h1>
        </div></td>
    </tr>
    <tr>
    <td class="table_body">
      <div align="center">
      <a href="<?=$_SERVER['PHP_SELF'];?>" title="upload more file(s)">
      <h3>Upload More</h3>
      </a>
        <div align="left"><br />
          <?
For($i=0$i <= $file_uploads-1$i++) {
    
    If(
$_FILES['file']['name'][$i]) {
        
$file=$i+1;
                Echo(
"<b>Direct URL #".$file.":</b> <a href=\"".$full_url.$file_name[$i]."\" target=\"_blank\">".$full_url.$file_name[$i]."</a><br /><br />\n");
    }
                
}

?>
          <br />
        <a href="/" title="upload more image(s)">
        <h3>Upload More</h3>
        </a>
        <p><span class="style1">&copy; 2008 Your name</span></p>
        </div></td>
  </tr>
</table>

<?} Else {?>

<?If($display_message){?>
    <div align="center" class="error_message"><?=$display_message;?></div>
    <br />
<?}?>

<form action="<?=$_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data" name="phuploader">
  <table align="center"class="table">
    <?For($i=0;$i <= $file_uploads-1;$i++) {?>
        <tr>
            <td class="table_body" width="22%"><b>select file(s):</b> </td>
            <td class="table_body" width="78%"><input type="file" name="file[]" size="30" /></td>
    </tr>
    <?}?>
    <tr>
        <td colspan="2" align="center" class="table_footer">
            <input type="hidden" name="submit" value="true" />
            <input name="Upload" type="submit" title="Upload Images" value="upload!"/></td>
    </tr>
</table>
</form>
<div align="center">
  <p><span class="style1">&copy; 2008 Your Name</span></p>
<!-- I worked very hard on this script, please do not delete this link back, its only on the home page.  Thanks! -->
<p><span class="style1">Powered by: <a href="http://dapezboy.org" title="Go to creator's page" target="_blank">AK design</a>'s power file uploader</span></p>
  <p>
    <?php ?>
  </p>
</div>
</body>
</html>
__________________


If I ever don't follow up on something, please feel free to PM me. I've got a lot going on, so a friendly reminder helps.
Reply With Quote
  #4 (permalink)  
Old 05-25-2008, 12:39 AM
Jaybirddesigns's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Helendale,ca
Posts: 133
Jaybirddesigns is on a distinguished road
Default

Wow very cool how long did it take you to write all that? Can't wait to try. It doesn't have to be index does it? just as long as it has the php extension right? it could be upload.php
Reply With Quote
  #5 (permalink)  
Old 05-27-2008, 10:09 PM
Jaybirddesigns's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Helendale,ca
Posts: 133
Jaybirddesigns is on a distinguished road
Default

Uploads Great thanks for the cool uploader. One thing though, I want the uploader to be able to replace the exsisting file in that specific directory. I replaced false under random file names but i get the error saying:

"The file: mccain.jpg exists on this server, please rename your file.
Your file(s) were not uploaded."

Is there a way around this?

Or do i need to right a script that removes the file. I beleive somthing like unset.

Thanks,

Jason
Reply With Quote
  #6 (permalink)  
Old 05-28-2008, 01:37 AM
pezboy45's Avatar
Mod/Design & Coder [Pro]
 
Join Date: Nov 2006
Location: Hawaii
Posts: 686
pezboy45 is on a distinguished road
Send a message via AIM to pezboy45 Send a message via MSN to pezboy45
Default

So, you want something like this:
http://img.dahoster.org/
(make an account)
It allows you to upload an image, and then upload it again, but replace it with a new one.
Hmm.
Well, I don't know how to do that.
But, I'm sure you could find what your looking for on:
http://hotscripts.com
__________________


If I ever don't follow up on something, please feel free to PM me. I've got a lot going on, so a friendly reminder helps.
Reply With Quote
  #7 (permalink)  
Old 05-30-2008, 03:35 AM
ishkey's Avatar
Moderator
 
Join Date: Aug 2007
Location: North GA USA
Posts: 1,360
ishkey will become famous soon enough
Default

see if this one works
Attached Files
File Type: zip maxUpload file.zip (16.7 KB, 191 views)
__________________
RIF= Reading Is Fun - Understanding Funner
cleandeck - lawn mower undercoating and wilmargraphite - graphite lubricants
Reply With Quote
  #8 (permalink)  
Old 05-31-2008, 03:32 AM
Jaybirddesigns's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Helendale,ca
Posts: 133
Jaybirddesigns is on a distinguished road
Default

Great! Can't wait to try.
Reply With Quote
  #9 (permalink)  
Old 05-31-2008, 07:58 PM
Jaybirddesigns's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Helendale,ca
Posts: 133
Jaybirddesigns is on a distinguished road
Default

Were do I put the Dir to where i want my upload to be.

function setUploadLocation($dir){
$this->uploadLocation = $dir;
}

Do i put it here like this?

function setUploadLocation(http://www.domain.com/images){
$this->uploadLocation = $dir;
}
Reply With Quote
  #10 (permalink)  
Old 05-31-2008, 10:05 PM
ishkey's Avatar
Moderator
 
Join Date: Aug 2007
Location: North GA USA
Posts: 1,360
ishkey will become famous soon enough
Default

try this one instead it is just for jpgs
it will create 3 directories for you - the code is easy to see
original
normal
thumbnail

you may have trouble with the .htaccess file - either mod it or leave it out
remember uploading threw http is slower. plus the program is doing alot. you can reload with the same name.
Attached Files
File Type: zip maximageupload.zip (11.0 KB, 175 views)
__________________
RIF= Reading Is Fun - Understanding Funner
cleandeck - lawn mower undercoating and wilmargraphite - graphite lubricants
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 11:58 AM.



mouseover mouseover mouseover