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
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...

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.