Page 1 of 1

XML Parsing & Filenames

Posted: Mon May 18, 2009 6:24 am
by fangonk
Hey guys I had some questions last week about using simpleXML to parse XML data. Now I want to do something similiar for image uploads. Basically I want to upload images via a flash uploader (http://www.uploadify.com/) and output file names to an XML file.
Here is what I have so far:

Code: Select all

 
<?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);
}
    
echo '1';
 
foreach($_FILES['Filedata']['name'] as $str;)
 
{
                                               
$fp = fopen('upload.xml', 'w');
fwrite($fp, $str);
fclose($fp);
header("Location: http://mydomain.com/imgUpload/"); 
 
}
 
?>
 
I am trying to create a string for each filename and then write those names directly to an XML file that already exists on the server. Besides any basic permissions problems can anyone see any reason why this wouldn't work?

Re: XML Parsing & Filenames

Posted: Tue May 19, 2009 3:41 pm
by Darhazer
You are using the wrong mode for fopen
Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
You have to use 'a+' instead. This will create file if it does not exist, or append to it.

Re: XML Parsing & Filenames

Posted: Tue May 19, 2009 6:56 pm
by fangonk
Like this?

Code: Select all

 
 
 $fp = fopen('upload.xml', 'a+');
 fwrite($fp, $str);
 fclose($fp);
 header("Location: http://mydomain.com/imgUpload/");
  
 
 

Re: XML Parsing & Filenames

Posted: Tue May 19, 2009 9:05 pm
by fangonk
OK, so here is the error I am getting when I run the script:
Warning: Invalid argument supplied for foreach()
The files are getting uploaded to the server but there is no xml file being written. Any ideas?

Re: XML Parsing & Filenames

Posted: Tue May 19, 2009 10:54 pm
by fangonk
I got it working!

For future reference:

Code: Select all

 
<?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);
 
?>