fagnonk Posted May 20, 2009 Share Posted May 20, 2009 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 More sharing options...
Masna Posted May 20, 2009 Share Posted May 20, 2009 A foreach loop is a loop that takes an array and cycles through it. $_FILES['file']['name'] is not an array: it's a string. Try this: foreach($_FILES as $str) { $fp = fopen('upload.xml', 'a+'); fwrite($fp, $str['name']); fclose($fp); } Link to comment https://forums.phpfreaks.com/topic/158849-xml-parsing-filenames/#findComment-837829 Share on other sites More sharing options...
michaellunsford Posted May 20, 2009 Share Posted May 20, 2009 There's only one $_FILES['Filedata']['name'] - you'd need to break it up. Here's how I'd do it <?php do { $fp=fopen('upload.xml','a+'); fwrite($fp,$_FILES[key($_FILES)]['name']); flclose($fp); } while(each($_FILES)); ?> Link to comment https://forums.phpfreaks.com/topic/158849-xml-parsing-filenames/#findComment-837830 Share on other sites More sharing options...
fagnonk Posted May 20, 2009 Author Share Posted May 20, 2009 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. Link to comment https://forums.phpfreaks.com/topic/158849-xml-parsing-filenames/#findComment-837833 Share on other sites More sharing options...
michaellunsford Posted May 20, 2009 Share Posted May 20, 2009 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)); ?> Link to comment https://forums.phpfreaks.com/topic/158849-xml-parsing-filenames/#findComment-837852 Share on other sites More sharing options...
fagnonk Posted May 20, 2009 Author Share Posted May 20, 2009 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. Link to comment https://forums.phpfreaks.com/topic/158849-xml-parsing-filenames/#findComment-837860 Share on other sites More sharing options...
fagnonk Posted May 20, 2009 Author Share Posted May 20, 2009 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'; ?> Link to comment https://forums.phpfreaks.com/topic/158849-xml-parsing-filenames/#findComment-837875 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.