Dosya upload scripti, karmaşık adda klasör oluşturtup dosyayı içine kaydetme?.

nuclearnsOnur Toker

Selam arkadaşlar,

Aşağıda script vereceğim, phUploader adındaki ücretsiz scripti kendime göre düzenledim ancak yapamadığım birşey var:

Şuandaki hali ile dosya upload edince dosya isminin sonuna .1276146535 gibi karışık rakamlar ekletiyorum, dosya var hatası vermemesi için. Ancak ben bunu dosya ismini değiştirmesi şeklinde değil de, karışık rakamlar içeren bir klasör açıp dosyayı bunun içine upload etmesini istiyorum.

Örneğin şuanda upload edince sonuç şöyle oluyor:

[code]http://localhost/files/SectigimDosyaAdi.1276146535.dosyaninUzantisi[/code]

Ben şöyle olmasını istiyorum:

[code]http://localhost/files/1276146535/SectigimDosyaAdi.dosyaninUzantisi[/code]

Bunu nasıl yapabilirim? PHP bilgim yetersiz olduğundan başaramadım.

Kodlar:

[code]<?php

$max_file_size="2048";
$max_combined_size="4096";
$file_uploads="2";
$websitename="File Upload";
$full_url="http://localhost/files/";
$folder="./files/";
$random_name=false;
$allow_types=array("jpg","gif","png","zip","rar","txt","doc");
$fullpath="";
$random=time()+rand(0,100000);


$error="";
$success="";
$display_message="";
$file_ext=array();

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

// Filename security cleaning. Do not modify.
function cln_file_name($string) {
    $cln_filename_find=array("/\.[^\.]+$/", "/[^\d\w\s-]/", "/\s\s+/", "/[-]+/", "/[_]+/");
    $cln_filename_repl=array("", ""," ", "-", "_");
    $string=preg_replace($cln_filename_find, $cln_filename_repl, $string);
    return trim($string);
}

if($_POST['submit']==true) {

    //Tally the size of all the files uploaded, check if it's over the ammount.   
    if(array_sum($_FILES['file']['size']) > $max_combined_size*1024) {
       
        $error.="<strong>FAILED:</strong> All Files <strong>REASON:</strong> Combined file size is to large.<br/>";
       
    // Loop though, verify and upload files.
    } else {

        // Loop through all the files.
        for($i=0; $i <= $file_uploads-1; $i++) {
           
            // if a file actually exists in this key
            if($_FILES['file']['name'][$i]) {

                //Get the file extension
                $file_ext[$i]=get_ext($_FILES['file']['name'][$i]);
               
                // Randomize file names
                if($random_name){
                    $file_name[$i]=time()+rand(0,100000);
                } else {
                    $file_name[$i]=cln_file_name($_FILES['file']['name'][$i]) . "." . $random;
                }
   
                // Check for blank file name
                if(str_replace(" ", "", $file_name[$i])=="") {
                   
                    $error.= "<strong>FAILED:</strong> ".$_FILES['file']['name'][$i]." <strong>REASON:</strong> Blank file name detected.<br/>";
               
                //Check if the file type uploaded is a valid file type.
                }    elseif(!in_array($file_ext[$i], $allow_types)) {
                               
                    $error.= "<strong>FAILED:</strong> ".$_FILES['file']['name'][$i]." <strong>REASON:</strong> Invalide file type.<br/>";
                               
                //Check the size of each file
                } elseif($_FILES['file']['size'][$i] > ($max_file_size*1024)) {
                   
                    $error.= "<strong>FAILED:</strong> ".$_FILES['file']['name'][$i]." <strong>REASON:</strong> File to large.<br/>";
                   
                // Check if the file already exists on the server..
                } elseif(file_exists($folder.$file_name[$i].".".$file_ext[$i])) {
   
                    $error.= "<strong>FAILED:</strong> ".$_FILES['file']['name'][$i]." <strong>REASON:</strong> File already exists.<br/>";
                   
                } else {
                   
                    if(move_uploaded_file($_FILES['file']['tmp_name'][$i],$folder.$file_name[$i].".".$file_ext[$i])) {
                       
                        $success.="<strong>SUCCESS:</strong> ".$_FILES['file']['name'][$i]."<br/>";
                        $success.="<strong>URL:</strong> <a href=\"".$full_url.$file_name[$i].".".$file_ext[$i]."\" target=\"_blank\">".$full_url.$file_name[$i].".".$file_ext[$i]."</a><br/><br/>";
                       
                    } else {
                        $error.="<strong>FAILED:</strong> ".$_FILES['file']['name'][$i]." <strong>REASON:</strong> General upload failure.<br/>";
                    }
                   
                }
                           
            } // if Files
       
        } // for
       
    } // else Total Size
   
    if(($error=="") AND ($success=="")) {
        $error.="<strong>FAILED:</strong> No files selected<br/>";
    }

    $display_message=$success.$error;

} // $_POST

/*
//================================================================================
* 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; ?> - Powered By phUploader</title>

<style type="text/css">
    body{
        background-color:#FFFFFF;
        font-family: Verdana, Arial, sans-serif;
        font-size: 12pt;
        color: #000000;
    }
   
    .message {
        font-family: Verdana, Arial, sans-serif;
        font-size: 11pt;
        color: #000000;
        background-color:#EBEBEB;
    }

    a:link, a:visited {
        text-decoration:none;
        color: #000000;
    }
   
    a:hover {
        text-decoration:none;
        color: #000000;
    }

    .table {
        border-collapse:collapse;
        border:1px solid #000000;
        width:450px;
    }
   
    .table_header {
        border:1px solid #000000;
        background-color:#C03738;
        font-family: Verdana, Arial, sans-serif;
        font-size: 11pt;
        font-weight:bold;
        color: #FFFFFF;
        text-align:center;
        padding:2px;
    }
   
    .upload_info {
        border:1px solid #000000;
        background-color:#EBEBEB;
        font-family: Verdana, Arial, sans-serif;
        font-size: 8pt;
        color: #000000;
        padding:4px;
    }

    .table_body {
        border:1px solid #000000;
        background-color:#EBEBEB;
        font-family: Verdana, Arial, sans-serif;
        font-size: 10pt;
        color: #000000;
        padding:2px;
    }

    .table_footer {
        border:1px solid #000000;
        background-color:#C03738;
        text-align:center;
        padding:2px;
    }

    input,select,textarea {
        font-family: Verdana, Arial, sans-serif;
        font-size: 10pt;
        color: #000000;
        background-color:#AFAEAE;
        border:1px solid #000000;
    }
   
    .copyright {
        border:0px;
        font-family: Verdana, Arial, sans-serif;
        font-size: 9pt;
        color: #000000;
        text-align:right;
    }
   
    form {
        padding:0px;
        margin:0px;
    }
</style>


<form action="<?=$_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data" name="phuploader">
<table align="center" class="table">
    <tr>
        <td class="table_header" colspan="2"><strong><?=$websitename;?></strong> </td>
    </tr>

    <?if($display_message){?>
    <tr>
        <td colspan="2" class="message">
        <br/>
            <?=$display_message;?>
        <br/>
        </td>
    </tr>
    <?}?>
   
    <tr>
        <td colspan="2" class="upload_info">
            <strong>Allowed Types:</strong> <?=implode($allow_types, ", ");?><br/>
            <strong>Max size per file:</strong> <?=$max_file_size?>kb.<br/>
            <strong>Max size for all files combined:</strong> <?=$max_combined_size?>kb.<br/>
        </td>
    </tr>
    <?for($i=0;$i <= $file_uploads-1;$i++) {?>
        <tr>
            <td class="table_body" width="20%"><strong>Select File:</strong> </td>
            <td class="table_body" width="80%"><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 type="submit" value=" Upload File(s) " /> &nbsp;
            <input type="reset" name="reset" value=" Reset form " onclick="window.location.reload(true);" />
        </td>
    </tr>
</table>
</form>

<table class="table" style="border:0px;" align="center">
    <tr>
        <td><div class="copyright">&copy;<a href="http://www.phphq.net/?script=phUploader" target="_blank" title="Uploader Powered By phUploader &lt;www.phphq.net&gt;">phUploader</a></div></td>
    </tr>
</table>
</body>
</html>[/code]

2010-06-09 07:53:11 Edit:2010-06-09 07:54:49

Aşağıdaki satır değişirse olur:

if(move_uploaded_file($_FILES['file']['tmp_name'][$i],$folder.$file_name[$i].".".$file_ext[$i])) {

aşağıdaki şekilde değişecek:


$folder .= $random.'/';

if(move_uploaded_file($_FILES['file']['tmp_name'][$i],$folder.$_FILES['file']['name'][$i])) {

bence Verdiğin kod gereğinden uzun. isim kontrolünde falan işi uzatmış. yabancılar bunu hep yapıyor. işi uzatınca başkası zor anlasın istiyorlar

Her programcı birgün Php yi tadacaktır
2010-06-09 22:18:19

Thanks

nuclearnsOnur Toker

Selamlar,

Çok teşşekkür ediyorum, ilerleyen dakikalarda deneyeceğim.

Ayrıca bir şey belirteyim: benim NucServ adında Apache, PHP, MySQL'in tek bir pakette toplandığı bir projem var, benzeri paketlere göre çok sade, hatasız ve düzenli.

Eğer Windows üzerinde geliştirmeler veya yayın yapıyorsanız deneyebilirsiniz.

http://www.nucserv.com

Site yöneticiside bu mesajı görürse ve sitede bu projeyi belirterek destek çıkarsa sevinirim. Windows kullanan büyük bir çoğunluk var ve bu çoğunlukta PHP öğrenmek istiyor, bu proje Windows üzerinde PHP için mükemmel. (Perl desteği henüz yok)

Ayrıca hem projemin sitesinin hem de diğer sitelerimin NucServ üzerinden 2 yıldır yayınlandığını belirteyim.

2010-06-09 22:28:14 Edit:2010-06-09 22:43:22

phpkodlari.com © 2009 Herkes Php öğrenecek
Eglence ve Oyun: Gamikro