Jump to content

Just trying to echo an uploaded file


CloudBreaker
Go to solution Solved by CloudBreaker,

Recommended Posts

I have an RFI form that a contractor will fill out and if he or she chooses to, can upload a pdf for reference.  The form works fine as well as the code for uploading the file which successfully puts the file information (file name, size and type) into the database as well as copying the file into a specified directory.  All I'm trying to accomplish now  is to have the file or files that have been uploaded (relating to the specific RFI)  listed or echoed at the bottom of the form.  Also, how would you make them downloadable by clicking on them once they are listed?  Towards the end of the code I always go blind or lose my sense of logic, so the confusion starts at line 155.

 

thanks

CB

<!DOCTYPE html>



<html>

	<head>
		<title>RFI Insert Test</title>

		<link href="form_styles.css" rel="stylesheet">
	</head>
	
		<body>
		
			<form id="form1" name="form1" method="post" action="rfi_form.php">
				<label for="name">RFI NO.</label><input type="text" name="name" id="name" placeholder="Ex: 003" required="required" />
				<br class="clear" /> 
				<label for="subject">Subject</label><input type="text" name="subject" id="subject" placeholder="Ex: Discontinued floor finish" />
				<br class="clear" /> 
				<label for="date_submit">Date submitted:</label><input type="date" name="date_submit" id="date_submit" />
				<br class="clear" /> 
				<label for="needed_by">Date Needed by:</label><input type="date" name="needed_by" id="needed_by" />
				<br class="clear" /> 
				<label for="question">Question:</label><textarea name="question" id="question" required="required" cols="100" rows="5"></textarea>
				<br class="clear" />
				<label for="gc_rsp">Contractor Suggestion:</label><textarea name="gc_rsp" id="gc_rsp" cols="100" rows="5"></textarea>
				<br class="clear" /> 
				<label for="owner_rsp">Owner Response:</label><textarea name="owner_rsp" id="owner_rsp" cols="100" rows="5"></textarea>
				<br class="clear" /> 
				<label for="engr_rsp">Engineer Response:</label><textarea name="engr_rsp" id="engr_rsp" cols="100" rows="5"></textarea>
				<br class="clear" /> 
				<label for="arch_rsp">Architect Response:</label><textarea name="arch_rsp" id="arch_rsp" cols="100" rows="5"></textarea>			
				<br class="clear" />
				<label for="final_by">Final Response by:</label><select name="final_by" id="final_by">
					<option></option>
					<option value="Architect">Architect</option>
					<option value="Owner">Owner</option>
					<option value="Structural Engineer">Structural Engineer</option>
					<option value="MEP Engineer">MEP Engineer</option>
					<option value="Civil Engineer">Civil Engineer</option>
					<option value="Landscape Architect">Landscape Architect</option>
				</select>
				<br class="clear" /> 
				<label for="date_returned">Date Returned:</label><input type="date" name="date_returned" id="date_returned" />
				<br class="clear" /> 
				<label for="status">Status:</label><select name="status" id="status">
					<option></option>
					<option value="CLOSED">CLOSED</option>
					<option value="at Architect">at Architect</option>
					<option value="at MEP Engineer">at MEP Engineer</option>
					<option value="at Structural Engineer">at Structural Engineer</option>
					<option value="at Civil Engineer">at Civil Engineer</option>
					<option value="at Landscape Architect">at Landscape Architect</option>
					<option value="at Owner">at Owner</option>
					<option value="at General Contractor">at General Contractor</option>
				</select>
				<br class="clear" /><br> 
				<input type="submit" name="submit_data" id="submit_data" value="Submit" />
			</form>
			
			
<?php
	
		$servername = "localhost";
		$username = "root";
		$password = "";
		$dbname = "hsa_project_hub";
		
	// Create connection
		$conn = new mysqli($servername, $username, $password, $dbname);
	// Check connection
		if ($conn->connect_error) {
		die("Connection failed: " . $conn->connect_error);
		} 
		
		
		
		

	//Enter info in database
		if(isset($_POST['submit_data'])){	
	
	//Getting the form information and saving in local variables 
		$name 			= mysqli_real_escape_string($conn,$_POST['name']);  
		$subject 		= mysqli_real_escape_string($conn,$_POST['subject']);  
		$date_submit 	= mysqli_real_escape_string($conn,$_POST['date_submit']);
		$needed_by 		= mysqli_real_escape_string($conn,$_POST['needed_by']); 
		$question		= mysqli_real_escape_string($conn,$_POST['question']);  
		$gc_rsp 		= mysqli_real_escape_string($conn,$_POST['gc_rsp']);  
		$owner_rsp 		= mysqli_real_escape_string($conn,$_POST['owner_rsp']);  
		$engr_rsp 		= mysqli_real_escape_string($conn,$_POST['engr_rsp']);  
		$arch_rsp 		= mysqli_real_escape_string($conn,$_POST['arch_rsp']);		 
		$final_by 		= mysqli_real_escape_string($conn,$_POST['final_by']);  
		$date_returned 	= mysqli_real_escape_string($conn,$_POST['date_returned']);  
		$status 		= mysqli_real_escape_string($conn,$_POST['status']); 

	
		
		
		$sql = " INSERT INTO rfis ( name, subject, date_submit, needed_by, question, gc_rsp, owner_rsp, engr_rsp, 
			arch_rsp, final_by, date_returned, status )  VALUES ( '$name', '$subject', '$date_submit', '$needed_by', '$question',
			'$gc_rsp', '$owner_rsp', '$engr_rsp', '$arch_rsp', '$final_by', '$date_returned', '$status' ) ";
			
		if (mysqli_query($conn, $sql)) {
			echo "New record created successfully";
			} else {
			echo "Error: " . $sql . "<br>" . mysqli_error($conn);
		}
		
		mysqli_close($conn);
		}

	
		//Choose files to be uploaded
		if(isset($_FILES['files'])){
			$errors= array();
			foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
				$rfi_fileName 	= $key.$_FILES['files']['name'][$key];
				$rfi_fileSize 	=$_FILES['files']['size'][$key];
				$file_tmp 		=$_FILES['files']['tmp_name'][$key];
				$rfi_fileType	=$_FILES['files']['type'][$key];	
				if($rfi_fileSize > 8097152){
					$errors[]='File size must be less than 8 MB';
				}		
				$query="INSERT into rfi_files (`rfi_fileName`,`rfi_fileSize`,`rfi_fileType`) VALUES('$rfi_fileName','$rfi_fileSize','$rfi_fileType'); ";
				$desired_dir="rfi_files";
				if(empty($errors)==true){
					if(is_dir($desired_dir)==false){
						mkdir("$desired_dir", 0700);		// Create directory if it does not exist
					}
					if(is_dir("$desired_dir/".$rfi_fileName)==false){
						move_uploaded_file($file_tmp,"rfi_files/".$rfi_fileName);
					}else{									//rename the file if another one exists
						$new_dir="rfi_files/".$rfi_fileName.time();
						 rename($file_tmp,$new_dir) ;				
					}
					mysqli_query($conn,$query);			
				}else{
						print_r($errors);
				}
			}
			if(empty($error)){
				echo "Success";
			}
		}
		?>
			<br>
			<form action="" method="POST" enctype="multipart/form-data">
			<input type="file" name="files[]" multiple="" />
			<input type="submit"/>
			</form>
		<?php

			
			$s = "Select rfi_files.rfi_filename, rfi_files.rfi_fileType\n"
			. "FROM rfi_files\n"
			. "WHERE rfi_files.id=22";
			$result=mysqli_query($conn,$s) or die('Error, query failed');
			$row=mysqli_fetch_assoc($result);
			
			$i=0;
			
			//execute query
			$run = mysqli_query($conn, $s);
			
			//assign file to variable
			while($row=mysqli_fetch_array($run)){
				
			$rfi_fileName =$row["rfi_fileName"];
			$i++;

			echo $rfi_fileName;
			}
		?>
			
					



			
			
		</body>
	
</html>
Edited by CloudBreaker
Link to comment
Share on other sites

I see you already have the echo $rfi_fileName; there. Sounds like you just need to build a url that would link the user to wherever you're storing the files.

echo '<a href="http://www.mydomain.com/files/' . $rfi_fileName . '">' . $rfi_fileName . '</a>';

Something like that maybe? It all just depends on where you are saving the files and whether or not you need protection on the files.

Link to comment
Share on other sites

  • Solution

Thanks iarp...you took me in the right direction.  this is what worked for me.  My string variable is different because I changed it.  It would not have made a difference.

<a href="rfi_files/<?php echo $rfiFilename;?>"><?php echo $rfiFilename;?></a>
Edited by CloudBreaker
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.