du kan bruge uploadify
http://www.uploadify.com/noget i den her stil, men hvis du ønsker at gemmen filen i databasen istedet for et "link" til filen, så skal du i kigge på noget blob
JS:
- $('#'+id).uploadify({
- 'uploader' : '/interface/js/jquery.uploadify-v2.1.4/uploadify.swf',
- 'script' : '/interface/js/jquery.uploadify-v2.1.4/uploadify.php,
- 'cancelImg' : '/interface/js/jquery.uploadify-v2.1.4/cancel.png',
- 'folder' : '/upload_dir/',
- 'displayData' : 'speed',
- 'auto' : true,
- 'multi' : false,
- 'onComplete' : function(event, ID, fileObj, response, data) {
- response = eval('(' + response + ')');
- if(response.uploaded){
- //okay
- console.log(response);
- }else{
- //failed
- console.log(response);
- }
- },
- 'onError' : function (event,ID,fileObj,errorObj) {
- console.log(event);
- console.log(ID);
- console.log(fileObj);
- console.log(errorObj);
- }
-
- });
- }
PHP:
- <?php
- /*
- Uploadify v2.1.4
- Release Date: November 8, 2010
-
- Copyright (c) 2010 Ronnie Garcia, Travis Nickels
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- */
-
-
- if (!empty($_FILES)) {
-
- $tempFile = $_FILES['Filedata']['tmp_name'];
- $targetPath = str_replace('//','/',$_SERVER['DOCUMENT_ROOT'] . "/". $_REQUEST['folder'] . '/');
- $filename = md5(microtime()."_".$_FILES['Filedata']['name']);
- $targetFile = str_replace('//','/',$targetPath) . $filename;
- $filelocation = str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetPath);
- $filetype = array_pop(explode(".", $_FILES['Filedata']['name']));
- $filesize = $_FILES['Filedata']['size'];
-
-
- if(move_uploaded_file($tempFile,$targetFile)){
- $lib->db->query("INSERT INTO ".dbPrefix."files (filelocation, filename) VALUES ('$targetPath', $filename')");
- echo json_encode(array(
- 'uploaded' => true,
- 'file' => $filelocation.$filename
- ));
-
- }else{
- echo json_encode(array('uploaded'=>false, 'msg'=>'Failed to move uploaded file', 'tempFile'=>$tempFile, 'targetFile'=>$targetFile));
- }
- }
- ?>
Indlæg senest redigeret d. 06.10.2011 12:03 af Bruger #3427