2013-10-10 31 views
0

首先,我對PHP很陌生,使用Jquery更好一些。我設法建立一個上傳iFrame上傳圖片到一個網上商店的Dropbox帳戶。如何獲取上傳文件前的ID

所以有人把T恤放在購物車中,然後需要上傳一些藝術品。客戶點擊「上傳」併發送到具有保管箱上傳腳本的iFrame。 iFrame的url是這樣的 - >http://webshop.com/dropbox/index.html?id=10102013-88981

到目前爲止好。然而問題是,當兩個人上傳一個具有相同名稱的文件時,第一個文件正在更新。所以我需要在文件前面有一個唯一的ID。該唯一標識是網址末尾的參數。

所以問題是如何獲得在URL的末尾的id和如何把它放在上傳的圖像?

Ideal可以是文件名的前綴或將所有內容存儲在自己的文件夾中。 我嘗試了幾件事,但我的知識是有限的,所以任何幫助非常感謝。

上傳腳本:

//Start the upload code. 

........ 
...... 
    if(sizeof($_FILES)===0){ 
     echo "<li>No files were uploaded.</li>"; 
     return; 
    } 

    foreach ($_FILES['ufiles']['name'] as $key => $value) { 

      if ($_FILES['ufiles']['error'][$key] !== UPLOAD_ERR_OK) { 
       echo $_FILES['ufiles']['name'][$key].' DID NOT upload.'; 
       return; 
      } 

      $tmpDir = uniqid('/tmp/DropboxUploader-'); 
      if (!mkdir($tmpDir)) { 
       echo 'Cannot create temporary directory!'; 
       return; 
      } 

      $tmpFile = $tmpDir.'/'.str_replace("/\0", '_', $_FILES['ufiles']['name'][$key]); 
      if (!move_uploaded_file($_FILES['ufiles']['tmp_name'][$key], $tmpFile)) { 
       echo $_FILES['ufiles']['name'][$key].' - Cannot rename uploaded file!'; 
       return; 
      } 

    try { 
      $uploader = new DropboxUploader($drop_account, $drop_pwd); 
      $uploader->upload($tmpFile, $drop_dir); 

      echo "<li>".$_FILES['ufiles']['name'][$key]."</li>" ; 

      // Clean up 
      if (isset($tmpFile) && file_exists($tmpFile)) 
       unlink($tmpFile); 

      if (isset($tmpDir) && file_exists($tmpDir)) 
       rmdir($tmpDir); 

     } catch(Exception $e) { 
      $error_msg = htmlspecialchars($e->getMessage()); 
      if($error_msg === 'Login unsuccessful.') { 
      echo '<li style="font-weight:bold;color:#ff0000;">Unable to log into Dropbox</li>'; 
      return; 
      } 
      if($error_msg === 'DropboxUploader requires the cURL extension.') { 
      echo '<li style="font-weight:bold;color:#ff0000;">Application error - contact admin.</li>'; 
      return; 
      } 

      echo '<li>'.htmlspecialchars($e->getMessage()).'</li>'; 
     } 

    } 

UPDATE的要求

形式:

<form class="formclass" id="ufileform" method="post" enctype="multipart/form-data"> 
     <fieldset> 

     <div><span class="fileinput"><input type="file" name="ufiles" id="ufiles" size="32" multiple /></span> 
</div> 
     </fieldset>    
     <button type="button" id="ubutton">Upload</button> 
     <button type="button" id="clear5" onclick="ClearUpload();">Delete</button> 

     <input type="hidden" name="id" id="prefix" value="" /> 
      </form> 

Upload.js(文件下載免費的腳本在互聯網上):

(function() { 

    if (window.FormData) { 
     var thefiles = document.getElementById('ufiles'), upload = document.getElementById('ubutton');//, password = document.getElementById('pbutton'); 
     formdata = new FormData(); 
     thefiles.addEventListener("change", function (evt) { 
     var files = evt.target.files; // FileList object 
     var i = 0, len = this.files.length, file; 
     for (; i < len; i++) { 
      file = this.files[i]; 
      if (isValidExt(file.name)) { //if the extension is NOT on the NOT Allowed list, add it and show it. 
       formdata.append('ufiles[]', file); 
       output.push('<li>', file.name, ' <span class="exsmall">', 
        bytesToSize(file.size, 2), 
        '</span></li>'); 
       document.getElementById('listfiles').innerHTML = '<ul>' + output.join('') + '</ul>'; 
      } 
     } 
     document.getElementById('filemsg').innerHTML = ''; 
     document.getElementById('filemsgwrap').style.display = 'none';   
     document.getElementById('ubutton').style.display = 'inline-block'; 
     document.getElementById('clear5').style.display = 'inline-block'; 
     }, false); 

     upload.addEventListener('click', function (evt) {     //monitors the "upload" button and posts the files when it is clicked 
     document.getElementById('progress').style.display = 'block'; //shows progress bar 
     document.getElementById('ufileform').style.display = 'none'; //hide file input form 
     document.getElementById('filemsg').innerHTML = '';    //resets the file message area 

     $.ajax({ 
      url: 'upload.php', 
      type: 'POST', 
      data: formdata, 
      processData: false, 
      contentType: false, 
      success: function (results) { 
       document.getElementById('ufileform').style.display = 'block'; 
       document.getElementById('progress').style.display = 'none'; 
       document.getElementById('filemsgwrap').style.display = 'block'; 
       document.getElementById('filemsg').innerHTML = '<ul>' + results + '</ul>'; 
       document.getElementById('listfiles').innerHTML = '<ul><li>Select Files for Upload</ul>'; 
       document.getElementById('ufiles').value = ''; 
       document.getElementById('ubutton').style.display = 'none'; 
       document.getElementById('clear5').style.display = 'none'; 
       formdata = new FormData(); 
       output.length = 0; 
      } 
     }); 
     }, false); 
    } else { 
    // document.getElementById('passarea').style.display = 'none'; 
     document.getElementById('NoMultiUploads').style.display = 'block'; 
     document.getElementById('NoMultiUploads').innerHTML = '<div>Your browser does not support this application. Try the lastest version of one of these fine browsers</div><ul><li><a href="http://www.mozilla.org" title="Mozilla Firefox"><img src="images/firefox-logo.png" alt="Mozilla Firefox" /></a></li><li><a href="http://www.google.com/chrome" title="Google Chrome"><img src="images/google-chrome-logo.png" alt="Google Chrome Firefox" /></a></li><li><a href="http://www.apple.com/safari/download/" title="Apple Safari"><img src="images/apple-safari-logo.png" alt="Apple Safari" /></a></li><li><a href="http://www.maxthon.com/" title="Maxthon"><img src="images/maxthon-logo.png" alt="Maxthon" /></a></li></ul>'; 
    } 

document.getElementById('multiload').style.display = 'block'; 
document.getElementById('ufileform').style.display = 'block'; 

}()); 

function ClearUpload() { //clears the list of files in the 'Files to Upload' area and resets everything to be ready for new upload 
    formdata = new FormData(); 
    output.length = 0; 
    document.getElementById('ufiles').value = ''; 
    document.getElementById('listfiles').innerHTML = 'Select Files for Upload'; 
    document.getElementById('ubutton').style.display = 'none'; 
    document.getElementById('clear5').style.display = 'none'; 
    document.getElementById('filemsgwrap').style.display = 'none'; 
} 

function getExtension(filename) { //Gets the extension of a file name. 
    var parts = filename.split('.'); 
    return parts[parts.length - 1]; 
} 

function isValidExt(filename) { //Compare the extension to the list of extensions that are NOT allowed. 
    var ext = getExtension(filename); 
    for(var i=0; i<the_ext.length; i++) { 
      if(the_ext[i] == ext) { 
       return false; 
       break; 
      } 
    } 
return true; 
} 

回答

1

改變這一行

$tmpFile = $tmpDir.'/'. $_POST['id'] . '-' . str_replace("/\0", '_', $_FILES['ufiles']['name'][$key]); 

注意其位置添加了$_POST['id']

編輯:在您的形式,你要留言更改爲$ _ POST

另外補充

<input type="hidden" name="id" value="<?=$_GET['id']; ?>" /> 
+0

好THX你的答案!問題是現在文件名是-file.jpg而不是1234-file.jpg。 'get [id]'是我嘗試過的,但不會從iFrame網址獲取id。這是否與它是iFrame的事實有關? – Meules

+0

更新...如果這不起作用發佈您的代碼爲您的