Jump to content

[SOLVED] File upload problem


lindm

Recommended Posts

Alright my problem. I have a html (form.html) page in a subdirectory of my site which lets the user submit a chosen file to a php file (writeform.php) in another directory for processing. Receive the following error:

 

Warning: file(file.txt) [function.file]: failed to open stream: No such file or directory in /Library/WebServer/Documents/htdocs/phptest2/script/writeform.php on line 131

 

Code part of form.html

<form action="../../script/writeform.php" method="post" enctype="multipart/form-data" name="form" id="form">
<input name="userfile" id="userfile" type="file" class="px10" size="6" /><INPUT name="SIE" TYPE="SUBMIT" class="px10" VALUE="Import"/>
</form>

 

Code part of writeform.php (includes row where error occurs)

$contents = file($_FILES['userfile']['name']);
    foreach($contents as $line_value)
    {CONTINUED CODE...

 

When I change $_FILES['userfile']['name'] to the aboslute path of a file it uses the file as expected, but when submitted from form.html the error occurs...does writeform.php not find the file?? What to do?

Link to comment
https://forums.phpfreaks.com/topic/81845-solved-file-upload-problem/
Share on other sites

Alright:

 

the function in writeform.php to extract certain data from the submitted file in form.html:

function calculate_range($cat, $bool, $range_min, $range_max)
{
    
if(isset($_POST["SIE"])){



$contents = file($_FILES['userfile']['name']);
    foreach($contents as $line_value)
    {
        list($_cat, $_bool, $_id, $num) = explode("\t", $line_value);

        $_cat = str_replace('#', '', $_cat);

        $number[$_cat][$_bool][$_id] = trim($num);
    }

    $total = 0;
    for($i = $range_min; $i <= $range_max; $i++)
    {
        if(isset($number[$cat][$bool][$i]))
        {
            $total += $number[$cat][$bool][$i];
        }
    }

    return $total;
}
}

 

Called like this and written to a new html file in writeform.php

$file = fopen($url, "w") or exit("Unable to open file!");
$stringData = 
'
calculate_range('UB', 0, 1090, 1099)
'
;
fwrite($file, $stringData);  
fclose($file);

 

Hope this helps you help med solve the problem. Strange thing is that when form.html and writeform.php are in the same directory all is fine but not when they are in different directories...

 

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.