[PHP] как переименовать файл если с таким именем уже существует?

Discussion in 'PHP' started by AnomaIy, 1 Dec 2012.

Thread Status:
Not open for further replies.
  1. AnomaIy

    AnomaIy New Member

    Joined:
    17 Nov 2012
    Messages:
    11
    Likes Received:
    0
    Reputations:
    0
    PHP:
    <?php
       
    // Edit upload location here
       
    $destination_path getcwd().DIRECTORY_SEPARATOR;

       
    $result 0;
       
       
    $target_path $destination_path basename$_FILES['myfile']['name']);

       if(@
    move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
          
    $result 1;
       }
       
       
    sleep(1);
    ?>

    <script language="javascript" type="text/javascript">window.top.window.stopUpload(<?php echo $result?>);</script>    
    как переименовать файл если с таким именем уже существует?
    или сразу имя файла на случайный
     
  2. AnomaIy

    AnomaIy New Member

    Joined:
    17 Nov 2012
    Messages:
    11
    Likes Received:
    0
    Reputations:
    0
    PHP:
    <?php
       
    // Edit upload location here
       
    $destination_path getcwd().DIRECTORY_SEPARATOR;

       
    $result 0;
       
       
    $target_path $destination_path basename$_FILES['myfile']['name']);

       
    $randval time();
       
    $newname $randval $_FILES["userfile" $filecounter]['name']; 
       
       
       if(@
    move_uploaded_file($_FILES['myfile']['tmp_name'], $newname)) {
          
    $result 1;
       }
       
       
    sleep(1);
    ?>

    <script language="javascript" type="text/javascript">window.top.window.stopUpload(<?php echo $result?>);</script> 

    но он не сохраняет расширение файла .jpg .gif .png ...
     
  3. AnomaIy

    AnomaIy New Member

    Joined:
    17 Nov 2012
    Messages:
    11
    Likes Received:
    0
    Reputations:
    0
    PHP:
    <?php
       
    // Edit upload location here
       
    $destination_path getcwd().DIRECTORY_SEPARATOR;

       
    $result 0;
       
       
    $target_path $destination_path basename$_FILES['myfile']['name']);

       
    $imagetypes = array(
        
    'image/png' => '.png',
        
    'image/gif' => '.gif',
        
    'image/jpeg' => '.jpg',
        
    'image/bmp' => '.bmp');
       
    $ext $imagetypes[$_FILES['myfile']['type']];

       
    $randval time();
       
    $newname $randval $ext
     
       if(@
    move_uploaded_file($_FILES['myfile']['tmp_name'], $newname)) {
          
    $result 1;
       }
       
       
    sleep(1);
    ?>

    <script language="javascript" type="text/javascript">window.top.window.stopUpload(<?php echo $result?>);</script>   
    получилось :)
     
  4. Gifts

    Gifts Green member

    Joined:
    25 Apr 2008
    Messages:
    2,494
    Likes Received:
    807
    Reputations:
    614
    https://forum.antichat.ru/thread68910.html
    https://forum.antichat.ru/thread360690.html
     
    _________________________
Thread Status:
Not open for further replies.