使用ajax和PHP获取最新文件时,避免部分上传的文件(通过FTP)
我知道这个标题有点奇怪,但我会到达那里.
我有一台相机连接到笔记本电脑.使用遥控拍摄,当摄影师拍照时,它会被保存到笔记本电脑硬盘上的文件夹中.在文件夹上设置了Automator(Mac OS X)操作,无论何时出现新文件,它都会调整大小并使用Transmit将其推送到FTP. 这是代码的来源. 我有一个显示最近拍摄照片的网页.它使用ajax重复检查是否已上传新文件,如果有,则加载新照片并将其与旧照片交叉淡入淡出.这是页面上运行的Javascript. (function() { var delay,refreshLoop; // Alias to setTimeout that reverses the parameters,making it much cleaner in code (for CoffeeScript) delay = function(time,callback) { return setTimeout(callback,time); }; // Function that drives the loop of refreshing photos refreshLoop = function(currentFolderState,refreshRate) { // Get the new folder state $.get("ajax/getFolderState.php",function(newFolderState) { // If the new folder state is different if (newFolderState !== currentFolderState) { // Get the newest photo $.get("ajax/getNewestPhoto.php",function(photoFilename) { var img; // Create the new image element img = $('<img class="new-photo"/>') // Append the src attribute to it after so it can BG load .attr('src',"/events/mindsmack/booth/cinco-de-mindsmack-2012/" + photoFilename) // When the image is loaded .load(function() { // Append the image to the photos container $('#photos').append(img); // Crossfade it with the old photo $('#photos .current-photo').fadeOut(); $('#photos .new-photo').fadeIn().removeClass("new-photo").addClass("current-photo"); }); }); } // Wait for the refresh rate and then repeat delay(refreshRate,function() { refreshLoop(newFolderState,refreshRate); }); }); }; // Document Ready $(function() { var refreshRate; // Load the first photo $.get("ajax/getNewestPhoto.php",function(photoFilename) { $('#photos').append("<img src='/events/mindsmack/booth/cinco-de-mindsmack-2012/" + photoFilename + "' class='current-photo' />"); }); refreshRate = 2000; // After the timeout delay(refreshRate,function() { // Get the initial folder state and kick off the loop $.get("ajax/getFolderState.php",function(initialFolderState) { refreshLoop(initialFolderState,refreshRate); }); }); }); }).call(this); 以下是在Javascript中调用的两个PHP文件 getFolderState.php <?php $path = $_SERVER['DOCUMENT_ROOT'] . "/events/mindsmack/booth/cinco-de-mindsmack-2012/"; // Get a directory listing of the path where the photos are stored $dirListing = scandir( $path ); // Echo an md5 hash of the directory listing echo md5(serialize($dirListing)); getNewestPhoto.php <?php $path = $_SERVER['DOCUMENT_ROOT'] . "/events/mindsmack/booth/cinco-de-mindsmack-2012/"; // Get a directory listing of the path where the photos are stored $listing = scandir($path); $modTime = 0; $mostRecent = ""; // Find the most recent file foreach ( $listing as $file ) { if ( is_file($path.$file) && $file !== ".DS_Store" && filectime($path.$file) > $modTime) { $modTime = filectime($path.$file); $mostRecent = $file; } } // Echo the most recent filename echo $mostRecent; 所有这些工作大多完美无瑕.我认为,问题是当文件处于上传过程中时触发循环.有时会拍摄一张照片,它只会在页面上显示.根本没有抛出错误,脚本继续运行得很好,图像文件实际上是在该状态下缓存的,这使我相信我的代码正在捕获正在进行的文件上传并且只显示文件的一部分已经上传的那一刻. 我不介意改变我的解决方案,如果我需要为了克服这个问题,我只是不确定该做什么. 编辑 根据下面的一条建议,我在我的getNewestPhoto.php中添加了代码,用于检查照片的文件大小,等待一下,然后再次检查.如果它们不同,它会返回并再次检查,直到文件大小相同.我希望这会抓住中上传的文件,因为文件大小会在循环之间发生变化,但即使照片出现部分呈现,文件大小的检查也没有抓住它. 这是我添加的代码 $currFilesize = filesize($path . $mostRecent); $newFilesize; while ( true ) { sleep(.5); $newFilesize = filesize($path . $mostRecent); if ( $newFilesize == $currFilesize) { break; } else { $currFilesize = $newFilesize; } } 我正在考虑(通过另一个建议)我需要在上传时添加某种锁定文件,以阻止代码刷新照片,并在上传完成后删除,但看到我没有运行任何类型的网页计算机上的服务器拴在相机上,我不知道如何实现这一目标.我会喜欢建议. 解决方法
有很多途径可以解决这个问题,比我建议的要好得多.我认为最简单,最快捷的解决方案是将FTP转换为tmp目录,并在传输完成后触发将文件移动到生产目录.你的本地自动化工作是否有与传输交易的机制?
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |