Hi,

How can I read a file content without uploading it to the server? I know that we use $handler = fopen('regular.csv', 'r'); to read an existing file on server but what if doesn't exist.

Obviously $_FILES is used to move file to server first but I don't have any folder writable and won't be given one. That's why I need to do this way.

Thanks

Recommended Answers

Try this to see if you can save to php temporary folder. I'm surprised your host doesn't allow file upload. Is it a free service?

<?php
if(isset($_FILES["file"])){
    if ($_FILES["file"]["error"] > 0){
        echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }else{
        $var = file_get_contents($_FILES["file"]["tmp_name"]);
        echo $var;  //test
    } …
Jump to Post

All 3 Replies

Member Avatar

Try this to see if you can save to php temporary folder. I'm surprised your host doesn't allow file upload. Is it a free service?

<?php
if(isset($_FILES["file"])){
    if ($_FILES["file"]["error"] > 0){
        echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }else{
        $var = file_get_contents($_FILES["file"]["tmp_name"]);
        echo $var;  //test
    }
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>...</title>
</head>
<body>
<form enctype="multipart/form-data" method="post">
<input name="file" type="file" />
<input name="sub" type="submit" value="go" />
</form>    
</body>
</html>

The temporary file (the one used in the form), will de deleted at the end of the script run. Works for me, but there again, I don't have such restrictions.

What kind of environment is your server running? If the host is using the suPHP module instead of the standard apache DSO module then your scripts execute as your user. So any files/directories may be writable, without having to be 777 etc.

Nice one diafol.

I've never though of reading $_FILES["file"]["tmp_name"] to be honest.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.