Hi...i would like to save and edit some excel files located in server using php. The coding below upload the file but when i open the page again...the message 'no file chosen' appears. Please help on this matter...thanks a lot...

<?php
error_reporting(E_ALL ^ E_NOTICE);
$_SESSION['Targetid'];
$_SESSION["Progressid"];
$conn = mysql_connect("localhost","pqa","");
mysql_select_db("pq",$conn);

if(count($_POST)>0) {

mysql_query("UPDATE progress set Attachment2='" . $_POST["Attachment2"] . "',
Attachment21='" . $_POST["Attachment21"] . "' WHERE Progressid='" . $_SESSION["Progressid"] . "'");
$message = "Record Modified Successfully";

}

$result = mysql_query("SELECT * FROM progress WHERE Progressid='" . $_SESSION["Progressid"] . "'");
$row= mysql_fetch_array($result);
?>
<html>
<head>
<title>Add New User</title>
<link rel="stylesheet" type="text/css" href="styles.css"/>
</head>
<body>
<form name="<?php echo $_SERVER['PHP_SELF']?>" method="post" action="">
<div style="width:500px;">
<div class="message"><?php if(isset($message)) { echo $message; } ?></div>
<div align="right" style="padding-bottom:5px;"><a href="list_progress2.php" class="link"><img alt='List' title='List' src='images/list.png' width='15px' height='15px'/> List Progress</a></div>
<p><b>1.Target</b></p>
<ENCTYPE="multipart/form-data">c.i.Preformatted Forms: <input type="file" name="Attachment2" MAXLENGTH=50 ALLOW="text/*" value="<?php echo $row['Attachment2']; ?>"><br>
<ENCTYPE="multipart/form-data">c.ii.School/Department Forms: <input type="file" name="Attachment21" MAXLENGTH=50 ALLOW="text/*" value="<?php echo $row['Attachment21']; ?>"><br>
<input type="hidden" name="Targetid" ><br>
<td colspan="2"><input type="submit" name="submit" value="Submit" class="btnSubmit"></td>
</div>
</form>
</body></html>

Dani AI

Generated

the "No file chosen" message is expected. Browsers clear file inputs on every load for security, and you cannot prefill a file input with a value. Also, value on <input type="file"> is ignored, ALLOW/MAXLENGTH are not valid there, and the form must have enctype="multipart/form-data". Store the uploaded filename on the server (and its path in your DB), then show a link to the saved file for later opening.

Basic pattern:

  • HTML: use a proper form and accept to hint allowed types (still validate on the server).
  • PHP: read $_FILES, validate type/size, use move_uploaded_file(), create a unique filename, and save that path in your progress row. Later, display a link to the saved file (not the file input).

Example:

<!-- upload form -->
<form method="post" enctype="multipart/form-data">
  <label>Preformatted Forms</label>
  <input type="file" name="Attachment2" accept=".xlsx,.xls">
  <label>School/Department Forms</label>
  <input type="file" name="Attachment21" accept=".xlsx,.xls">
  <button type="submit" name="submit">Submit</button>
</form>

<?php
if (!empty($_POST['submit'])) {
  foreach (['Attachment2','Attachment21'] as $field) {
    if (!empty($_FILES[$field]['name'])) {
      $ext = strtolower(pathinfo($_FILES[$field]['name'], PATHINFO_EXTENSION));
      if (!in_array($ext, ['xlsx','xls'])) { die('Invalid file type'); }
      $safe = uniqid($field.'_').'.'.$ext;
      $dir  = __DIR__.'/uploads';
      if (!is_dir($dir)) { mkdir($dir, 0755, true); }
      if (!move_uploaded_file($_FILES[$field]['tmp_name'], "$dir/$safe")) { die('Upload failed'); }
      // TODO: save $safe in your DB column matching $field using PDO/mysqli
    }
  }
}

To "click and edit": provide a link to the stored file. Users open it in Excel, edit locally, and re-upload a new version. Server-side in-place editing requires a library (e.g., PhpSpreadsheet) or an online editor integration. Also consider versioning, file size limits, MIME checks (finfo), storing uploads outside webroot, and moving away from mysql_* to PDO/mysqli. ’s CSV approach is for importing into the DB, which you said you do not need right now.

Recommended Answers

All 2 Replies

You need to do this with a csv file.

    //Upload File
51  if (isset($_POST['submit'])) {
52      if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
53          echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
54          echo "<h2>Displaying contents:</h2>";
55          readfile($_FILES['filename']['tmp_name']);
56      }
57   
58      //Import uploaded file to Database
59      $handle = fopen($_FILES['filename']['tmp_name'], "r");
60   
61      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
62          $import="INSERT into tablename(item1,item2,item3,item4,item5) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]')";
63   
64          mysql_query($import) or die(mysql_error());
65      }
66   
67      fclose($handle);
68   
69      print "Import done";
70   
71      //view upload form
72  }

Hi...thanks a lot for your fast reply...sorry for not being specific...actually i just want to upload the excel file to server and whenever i want to edit the file, i just open the page and click on the file to edit it...currently there is no need to import uploaded file to database...please advise,thanks.

Be a part of the DaniWeb community

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