Upload et le traitement de fichiers multiples

mcfive5
Invité n'ayant pas de compte PHPfrance

23 mai 2012, 14:16

Bonjour !

Je sollicite votre aide pour mettre en place l'upload et le traitement de fichiers multiples en PHP, je suis débutant et je suis un peu perdu !

Après recherches j'ai décidé de mettre cela en place grâce à FancyUpload.

http://digitarald.de/project/fancyupload/


Pour l'instant, j'ai réussi à faire fonctionner l'upload multiple de fichiers vers un dossier du serveur.

Avec le fonctionnement actuel, une fois l'upload des fichiers correctement effectué le fichier php gérant l'upload des fichiers retourne des messages vers la page contenant le formulaire permettant l'upload.

Seulement moi une fois l'upload des fichiers terminé, je souhaiterai être redirigé vers un fichier php dans le quel je pourrais récupérer les références sur les fichiers que je viens d'uploader et effectuer diverses traitements sur ceux-ci et des affichages.

Les fichier sources : http://www.partage-facile.com/0TZZ763KX ... d.zip.html

Merci d'avance pour vodre aide ! :D


Voici le code :
<div class="container">

		<div>
			<form action="server/script.php" method="post" enctype="multipart/form-data" id="form-demo">

	<fieldset id="demo-fallback">
		<p>
		    Problème !
		</p>
	</fieldset>

	<div id="demo-status" class="hide">
		<p>
			<a href="#" id="demo-browse">Browse Files</a> |
			<a href="#" id="demo-clear">Clear List</a> |
			<a href="#" id="demo-upload">Start Upload</a>
		</p>
		<div>
			<strong class="overall-title"></strong><br />
			<img src="../../assets/progress-bar/bar.gif" class="progress overall-progress" />
		</div>
		<div>
			<strong class="current-title"></strong><br />
			<img src="../../assets/progress-bar/bar.gif" class="progress current-progress" />
		</div>
		<div class="current-text"></div>
	</div>

	<ul id="demo-list"></ul>

</form>		</div>


	</div>
[javascript]
<script type="text/javascript" src="source/mootools.js"></script>

<script type="text/javascript" src="source/Swiff.Uploader.js"></script>

<script type="text/javascript" src="source/Fx.ProgressBar.js"></script>

<script type="text/javascript" src="source/FancyUpload2.js"></script>

<script type="text/javascript">
//<![CDATA[

window.addEvent('domready', function() { // wait for the content

// our uploader instance

var up = new FancyUpload2($('demo-status'), $('demo-list'), { // options object
// we console.log infos, remove that in production!!
verbose: true,

// url is read from the form, so you just have to change one place
url: $('form-demo').action,

// path to the SWF file
path: 'source/Swiff.Uploader.swf',

// remove that line to select all files, or edit it, add more items
typeFilter: {
//'Images (*.jpg, *.jpeg, *.gif, *.png)': '*.jpg; *.jpeg; *.gif; *.png'
'Flux (*.txt, *.TXT, *.dat, *.xml)': '*.txt; *.TXT; *.dat; *.xml'
},

// this is our browse button, *target* is overlayed with the Flash movie
target: 'demo-browse',

// graceful degradation, onLoad is only called if all went well with Flash
onLoad: function() {
$('demo-status').removeClass('hide'); // we show the actual UI
$('demo-fallback').destroy(); // ... and hide the plain form

// We relay the interactions with the overlayed flash to the link
this.target.addEvents({
click: function() {
return false;
},
mouseenter: function() {
this.addClass('hover');
},
mouseleave: function() {
this.removeClass('hover');
this.blur();
},
mousedown: function() {
this.focus();
}
});

// Interactions for the 2 other buttons

$('demo-clear').addEvent('click', function() {
up.remove(); // remove all files
return false;
});

$('demo-upload').addEvent('click', function() {
up.start(); // start upload
return false;
});
},

// Edit the following lines, it is your custom event handling

/**
* Is called when files were not added, "files" is an array of invalid File classes.
*
* This example creates a list of error elements directly in the file list, which
* hide on click.
*/
onSelectFail: function(files) {
files.each(function(file) {
new Element('li', {
'class': 'validation-error',
html: file.validationErrorMessage || file.validationError,
title: MooTools.lang.get('FancyUpload', 'removeTitle'),
events: {
click: function() {
this.destroy();
}
}
}).inject(this.list, 'top');
}, this);
},

/**
* This one was directly in FancyUpload2 before, the event makes it
* easier for you, to add your own response handling (you probably want
* to send something else than JSON or different items).
*/
onFileSuccess: function(file, response) {
var json = new Hash(JSON.decode(response, true) || {});

if (json.get('status') == '1') {
file.element.addClass('file-success');
//file.info.set('html', '<strong>Image was uploaded:</strong> ' + json.get('width') + ' x ' + json.get('height') + 'px, <em>' + json.get('mime') + '</em>)');
file.info.set('html', '<strong>File was uploaded</strong> ');
} else {
file.element.addClass('file-failed');
file.info.set('html', '<strong>An error occured:</strong> ' + (json.get('error') ? (json.get('error') + ' #' + json.get('code')) : response));
}
},

/**
* onFail is called when the Flash movie got bashed by some browser plugin
* like Adblock or Flashblock.
*/
onFail: function(error) {
switch (error) {
case 'hidden': // works after enabling the movie and clicking refresh
alert('To enable the embedded uploader, unblock it in your browser and refresh (see Adblock).');
break;
case 'blocked': // This no *full* fail, it works after the user clicks the button
alert('To enable the embedded uploader, enable the blocked Flash movie (see Flashblock).');
break;
case 'empty': // Oh oh, wrong path
alert('A required file was not found, please be patient and we fix this.');
break;
case 'flash': // no flash 9+ :(
alert('To enable the embedded uploader, install the latest Adobe Flash plugin.')
}
}

});

});
//]]>
</script>[/javascript]
<?php

$result = array();

$result['time'] = date('r');
$result['addr'] = substr_replace(gethostbyaddr($_SERVER['REMOTE_ADDR']), '******', 0, 6);
$result['agent'] = $_SERVER['HTTP_USER_AGENT'];

if (count($_GET)) {
	$result['get'] = $_GET;
}
if (count($_POST)) {
	$result['post'] = $_POST;
}
if (count($_FILES)) {
	$result['files'] = $_FILES;
}

// we kill an old file to keep the size small
if (file_exists('script.log') && filesize('script.log') > 102400) {
	unlink('script.log');
}

$log = @fopen('script.log', 'a');
if ($log) {
	fputs($log, print_r($result, true) . "\n---\n");
	fclose($log);
}


// Validation

$error = false;

if (!isset($_FILES['Filedata']) || !is_uploaded_file($_FILES['Filedata']['tmp_name'])) {
	$error = 'Invalid Upload';
}

// Processing


move_uploaded_file($_FILES['Filedata']['tmp_name'], '../uploads/' . $_FILES['Filedata']['name']);
$return['src'] = '/uploads/' . $_FILES['Filedata']['name'];

if ($error) {

	$return = array(
		'status' => '0',
		'error' => $error
	);

} else {

	$return = array(
		'status' => '1',
		'name' => $_FILES['Filedata']['name']
	);

}


// Output

/**
 * Again, a demo case. We can switch here, for different showcases
 * between different formats. You can also return plain data, like an URL
 * or whatever you want.
 */

if (isset($_REQUEST['response']) && $_REQUEST['response'] == 'xml') {
	echo '<response>';
	foreach ($return as $key => $value) {
		echo "<$key><![CDATA[$value]]></$key>";
	}
	echo '</response>';
} else {
	// header('Content-type: application/json');

	echo json_encode($return);
}

?>

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 8758 Messages

23 mai 2012, 16:43

salut,

utilise les sessions pour garder les références vers les films
utilise la fonction header pour aller sur l'autre page.

@+
Il en faut peu pour être heureux ......