Jump to content

XML Parsing & Filenames


fagnonk

Recommended Posts

I am trying to modify a flash uploader (uploadify.com), so that it will create an XML list of all the file names uploaded.

 

Here is my upload.php:

<?php
// JQuery File Upload Plugin v1.4.1 by RonnieSan - (C)2009 Ronnie Garcia
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

// Uncomment the following line if you want to make the directory if it doesn't exist
// mkdir(str_replace('//','/',$targetPath), 0755, true);

move_uploaded_file($tempFile,$targetFile);
}

foreach($_FILES['Filedata']['name'] as $str) {
$fp = fopen('upload.xml', 'a+');
fwrite($fp, $str);
fclose($fp);
}

?>

 

Here is the error I am getting:

Warning: Invalid argument supplied for foreach() 

 

The files are uploading fine but I can't get it to write the XML file so there must be something wrong with the way I formed the loop. Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/158849-xml-parsing-filenames/
Share on other sites

Ok So I tried it both ways:

 

<?php
// JQuery File Upload Plugin v1.4.1 by RonnieSan - (C)2009 Ronnie Garcia
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

// Uncomment the following line if you want to make the directory if it doesn't exist
// mkdir(str_replace('//','/',$targetPath), 0755, true);

move_uploaded_file($tempFile,$targetFile);
}

?>

<?php
do {
   $fp=fopen('upload.xml','a+');
   fwrite($fp,$_FILES[key($_FILES)]['name']);
   flclose($fp);
} while(each($_FILES));
?>

 

and:

<?php
// JQuery File Upload Plugin v1.4.1 by RonnieSan - (C)2009 Ronnie Garcia
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

// Uncomment the following line if you want to make the directory if it doesn't exist
// mkdir(str_replace('//','/',$targetPath), 0755, true);

move_uploaded_file($tempFile,$targetFile);
}

$str = $_FILES['Filedata']['name'];
$fp = fopen('upload.xml', 'a+');
fwrite($fp, $str);
fclose($fp);

?>

 

The result is the same, the files are getting uploaded but there is no xml file creation.

by the way, I have a typo, flclose should be fclose. Considering you didn't get an error, I'm thinking you might have error reporting turned off.

 

http://us.php.net/manual/en/function.error-reporting.php

 

Add that to the top of the code and add an echo as well. It should present you with your filename on success -- or it should whine if it fails.

 

<?php
error_reporting(E_ALL);
do {
   $fp=fopen('upload.xml','a+');
   fwrite($fp,$_FILES[key($_FILES)]['name']);
   fclose($fp);
   echo $_FILES[key($_FILES)]['name'])."<br />\r\n";
} while(each($_FILES));
?>

There is probably no error because I am using a flash uploader that is separate from upload.php. I have to check for errors by directly accessing upload.php, I just forgot to check it last time.

 

You can look at the uploader here:

http://cstang.hk/imgupload/uploadify-multi-single.php

 

The actual upload.php:

http://cstang.hk/imgupload/uploadify/upload.php

 

When I changed the upload.php file, I am getting a new error:

Parse error: syntax error, unexpected ')', expecting ',' or ';'

 

I tried removing the ')' but this just gave me an undefined index error.

I got it working, for future reference, here it is:

<?php
// JQuery File Upload Plugin v1.4.1 by RonnieSan - (C)2009 Ronnie Garcia
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

// Uncomment the following line if you want to make the directory if it doesn't exist
// mkdir(str_replace('//','/',$targetPath), 0755, true);

move_uploaded_file($tempFile,$targetFile);
}

$str = $_FILES['Filedata']['name'];
$fp = fopen('upload.xml', 'a+');
fwrite($fp, $str);
fclose($fp);

echo '1';

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.