XML Parsing & Filenames

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fangonk
Forum Newbie
Posts: 21
Joined: Thu May 14, 2009 6:43 am

XML Parsing & Filenames

Post 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?
Last edited by Benjamin on Mon May 18, 2009 10:08 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: XML Parsing & Filenames

Post 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.
fangonk
Forum Newbie
Posts: 21
Joined: Thu May 14, 2009 6:43 am

Re: XML Parsing & Filenames

Post by fangonk »

Like this?

Code: Select all

 
 
 $fp = fopen('upload.xml', 'a+');
 fwrite($fp, $str);
 fclose($fp);
 header("Location: http://mydomain.com/imgUpload/");
  
 
 
Last edited by Benjamin on Tue May 19, 2009 8:28 pm, edited 1 time in total.
Reason: Changed code type from text to php.
fangonk
Forum Newbie
Posts: 21
Joined: Thu May 14, 2009 6:43 am

Re: XML Parsing & Filenames

Post 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?
fangonk
Forum Newbie
Posts: 21
Joined: Thu May 14, 2009 6:43 am

Re: XML Parsing & Filenames

Post 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);
 
?>
 
Last edited by Benjamin on Wed May 20, 2009 7:53 am, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply