2013-10-10 34 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

更新...如果这不起作用发布您的代码为您的