# PHP upload \> Write to XML

**URL:** <https://forum.kirupa.com/t/php-upload-write-to-xml/228020>\
**Category:** programming\
**Created:** [June 4, 2007, 6:04pm UTC](https://forum.kirupa.com/t/php-upload-write-to-xml/228020 "2007-06-04T18:04:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gregaz](https://avatars.discourse-cdn.com/v4/letter/g/fbc32d/32.png) [@Gregaz](https://forum.kirupa.com/u/Gregaz)\
**Post date:** [June 4, 2007, 6:04pm UTC](https://forum.kirupa.com/t/php-upload-write-to-xml/228020/1 "2007-06-04T18:04:34Z")

</div>

Hey.  
I got an upload function that does it job as it should,  
then i got a form to input text to an XML-file.  
I then load the XML-file to flash.  
The thing is that as it is now I have to input  
the uploaded image-file´s name, after I´ve uploaded it,  
into an input-field in another form to send into my XML-file.  
Is there any U´shocker who can help me out with an  
solution to combine the upload with in “send to xml” function,  
so that I won´t have to type in the name of the image-fil.  
There has gotta be a away to grab the image-file´s name  
and send it? right?  
This is the upload script

```php

<?php
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
    /* SUBMITTED INFORMATION - use what you need
     * temporary filename (pointer): $imgfile
     * original filename : $imgfile_name
     * size of uploaded file : $imgfile_size
     * mime-type of uploaded file : $imgfile_type
     */
     /*== upload directory where the file will be stored 
          relative to where script is run ==*/
 
    $uploaddir = ".";
 
    /*== get file extension (fn at bottom of script) ==*/
    /*== checks to see if image file, if not do not allow upload ==*/
    $pext = getFileExtension($imgfile_name);
    $pext = strtolower($pext);
    if (($pext != "jpg") && ($pext != "jpeg"))
    {
        print "<h1>ERROR</h1>Image Extension Unknown.<br>";
        print "<p>Please upload only a JPEG image with the extension .jpg or .jpeg ONLY<br><br>";
        print "The file you uploaded had the following extension: $pext</p>
";
        /*== delete uploaded file ==*/
        unlink($imgfile);
        exit();
    }
 
    //-- RE-SIZING UPLOADED IMAGE
    /*== only resize if the image is larger than 250 x 200 ==*/
    $imgsize = GetImageSize($imgfile);
    /*== check size 0=width, 1=height ==*/
    if (($imgsize[0] > 200) || ($imgsize[1] > 200)) 
    {
        /*== temp image file -- use "tempnam()" to generate the temp
             file name. This is done so if multiple people access the 
            script at once they won't ruin each other's temp file ==*/
        $tmpimg = tempnam("/tmp", "MKUP");
        /*== RESIZE PROCESS
             1. decompress jpeg image to pnm file (a raw image type) 
             2. scale pnm image
             3. compress pnm file to jpeg image
        ==*/
 
        /*== Step 1: djpeg decompresses jpeg to pnm ==*/
        system("djpeg $imgfile >$tmpimg");
 
        /*== Steps 2&3: scale image using pnmscale and then
             pipe into cjpeg to output jpeg file ==*/
        system("pnmscale -xy 200 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");
        /*== remove temp image ==*/
        unlink($tmpimg);
    }
    /*== setup final file location and name ==*/
    /*== change spaces to underscores in filename ==*/
    $final_filename = str_replace(" ", "_", $imgfile_name);
    $newfile = $uploaddir . "/$final_filename";
 
    /*== do extra security check to prevent malicious abuse==*/
    if (is_uploaded_file($imgfile))
    {
       /*== move file to proper directory ==*/
       if (!copy($imgfile,"$newfile")) 
       {
          /*== if an error occurs the file could not
               be written, read or possibly does not exist ==*/
          print "Error Uploading File.";
          exit();
       }
     }
    /*== delete the temporary uploaded file ==*/
    unlink($imgfile);
 
    print("<img src=\"$final_filename\">");
 $upPic = "<img src=\"$final_filename\">";
 
    /*== DO WHATEVER ELSE YOU WANT
         SUCH AS INSERT DATA INTO A DATABASE ==*/
}
?>

```

the actual form for the upload

```php

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="150000">
    <p>Upload Image: <input type="file" name="imgfile"><br>
    <font size="1">Click browse to upload a local file</font><br>
    <br>
    <input type="submit" value="Upload Image">
    </form>

```

This is the form for the “send to xml”

```php

<form action="processForm.php" method="post">
<fieldset>
<label for="path">Path:</label> <input type="text" id="path" name="path" value="$upPic" /> <br />
<select name="action">
<option value="ins">Insert</option>
<option value="del">Delete</option>
</select>
<input type="submit" />
</fieldset>
</form>

```

this is the processForm.php - file

```php

<?
$songs = Array();
function start_element($parser, $name, $attrs){
 global $songs;
 if($name == "song"){
  array_push($songs, $attrs);
 }
}
function end_element ($parser, $name){}
$playlist_string = file_get_contents("playlist.xml");
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "end_element");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse($parser, $playlist_string) or die("Error parsing XML document.");
print "<br />";
if($_POST['action'] == "ins"){
 array_push($songs, Array(
    "path" => $_POST['path']));
 $songs_final = $songs;
}else if($_POST['action'] == "del"){
 $songs_final = Array();
 foreach($songs as $song){
  if($song['path'] != $_POST['path']){
   array_push($songs_final, $song);
  }
 }
}
$write_string = "<songs>";
foreach($songs_final as $song){
 $write_string .= "<song path=\"$song[path]\" />";
}
$write_string .= "</songs>";
$fp = fopen("playlist.xml", "w+");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
print "<em>Song inserted or deleted successfully :)</em><br />";
print "<a href=\"upload2.php\" title=\"return\">Return</a>";
?>

```

any ideas?

( sorry for the massive amount of code )
