Christian Burgos
Christian Burgos

Reputation: 1591

opening and viewing a file in php

how do i open/view for editing an uploaded file in php?

i have tried this but it doesn't open the file.

$my_file = 'file.txt';
$handle = fopen($my_file, 'r');
$data = fread($handle,filesize($my_file));

i've also tried this but it wont work.

$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file);
$data = 'This is the data';
fwrite($handle, $data);

what i have in mind is like when you want to view an uploaded resume,documents or any other ms office files like .docx,.xls,.pptx and be able to edit them, save and close the said file.

edit: latest tried code...

 <?php 
 // Connects to your Database 
 include "configdb.php"; 

 //Retrieves data from MySQL 
 $data = mysql_query("SELECT * FROM employees") or die(mysql_error()); 
 //Puts it into an array 
 while($info = mysql_fetch_array( $data )) 
 { 

 //Outputs the image and other data
 //Echo "<img src=localhost/uploadfile/images".$info['photo'] ."> <br>"; 
 Echo "<b>Name:</b> ".$info['name'] . "<br> "; 
 Echo "<b>Email:</b> ".$info['email'] . " <br>"; 
 Echo "<b>Phone:</b> ".$info['phone'] . " <hr>"; 
 //$file=fopen("uploadfile/images/".$info['photo'],"r+");
 $file=fopen("Applications/XAMPP/xamppfiles/htdocs/uploadfile/images/file.odt","r") or exit("unable to open file");;
 }
 ?> 

i am getting the error:

Warning: fopen(Applications/XAMPP/xamppfiles/htdocs/uploadfile/images/file.odt): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/uploadfile/view.php on line 17
unable to open file

the file is in that folder, i don't know it wont find it.

Upvotes: 1

Views: 397

Answers (2)

Tivie
Tivie

Reputation: 18923

For text based files you can use file_get_contents and file_put_contents

However, ODT files are zip compressed XML files so you need to decompress them first:

$zip = new ZipArchive;
$res = $zip->open('Applications/XAMPP/xamppfiles/htdocs/uploadfile/images/file.odt');
if ($res === TRUE) {
  $zip->extractTo('/tmp/myOdt.xml');
  $zip->close();
}

$data = file_get_contents('/tmp/myOdt.xml');

$data .= 'add this to the end';

file_put_contents('path/to/file.txt', $data);

However, it seems your problem regards the actual path of the file. Try using a relative path instead: "images/file.odt"

Upvotes: 0

willy
willy

Reputation: 1490

It might be either:

  1. A permissions issue on the server. If it's a linux machine, try chmod 754 Applications/XAMPP/xamppfiles/htdocs/uploadfile/images/file.odt (you may need root).

  2. An issue with apache (or whatever webserver you might be using). Make sure you've defined a directory entry in the config file for that site. Documentation here.

Although, if I recall properly, an odt file will just be binary data rather than the text information. That might be what you're looking for, I don't know. If you just want to read the actual text, and you aren't interested in using some library to extract it, you need to save it as plain text. If you're actually trying to edit these files in the browser, you're gonna need a lot more than just fopen.

Upvotes: 1

Related Questions