Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

Sign in to follow this  
shadowk

[Resolvido] Guardar imagem PHP

Recommended Posts

shadowk

Boas!

Eu estou a fazer o meu projecto de final de curso e necessitava de uma ajuda.

Eu tenho um sistema de upload feito, só que eu em vez de guardar a imagem directamente na base de dados vou guardar só o caminho para a imagem, o problema é que essa coisa tão simples deu-me um nó no cerebro  :)

<?php

include("ligacao.php");

$upload_errors = array(
  UPLOAD_ERR_OK 		=> "No errors.",
  UPLOAD_ERR_INI_SIZE  	=> "Larger than upload_max_filesize.",
  UPLOAD_ERR_FORM_SIZE 	=> "Larger than form MAX_FILE_SIZE.",
  UPLOAD_ERR_PARTIAL 	=> "Partial upload.",
  UPLOAD_ERR_NO_FILE 	=> "No file.",
  UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
  UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
  UPLOAD_ERR_EXTENSION 	=> "File upload stopped by extension."
);

if(isset($_POST['submit'])) {
$tmp_file = $_FILES['file_upload']['tmp_name'];
$target_file = basename($_FILES['file_upload']['name']);
$upload_dir = "uploads";

if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file)) {
	$message = "" .$upload_dir. "/" .$target_file. "";
} else {
	$error = $_FILES['file_upload']['error'];
	$message = $upload_errors[$error];
}
}

?>
<html>
<head>
	<title>Upload</title>
</head>
<body>


	<?php if(!empty($message)) { echo "<p>{$message}</p>"; } ?>
	<form action="upload.php" enctype="multipart/form-data" method="POST">

	  <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
	  <input type="file" name="file_upload" />

	  <input type="submit" name="submit" value="Upload" />
	</form>

</body>
</html>

BD: upload

Tabela: upload

Campo: img

Este código guarda a imagem numa pasta e imprime o caminho da imagem, eu já tentei guardar o caminho na BD só que não guardava...  :D

Se alguem me podesse ajudar agradessia imenso!

Obrigado desde já  :)


" THE SECRET OF CREATIVITY IS KNOWING HOW TO HIDE YOUR SOURCES "

Share this post


Link to post
Share on other sites
shadowk

Era este o código, mas não está nada bem  :D

<?php

include("ligacao.php");

$upload_errors = array(
  UPLOAD_ERR_OK 		=> "No errors.",
  UPLOAD_ERR_INI_SIZE  	=> "Larger than upload_max_filesize.",
  UPLOAD_ERR_FORM_SIZE 	=> "Larger than form MAX_FILE_SIZE.",
  UPLOAD_ERR_PARTIAL 	=> "Partial upload.",
  UPLOAD_ERR_NO_FILE 	=> "No file.",
  UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
  UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
  UPLOAD_ERR_EXTENSION 	=> "File upload stopped by extension."
);

if(isset($_POST['submit'])) {
$tmp_file = $_FILES['file_upload']['tmp_name'];
$target_file = basename($_FILES['file_upload']['name']);
$upload_dir = "uploads";

if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file)) {
	$message = "" .$upload_dir. "/" .$target_file. "";
        $upload = $message;
        $query = "INSERT INTO upload (
			img
		) VALUES (
			'{$upload}'
		)";

} else {
	$error = $_FILES['file_upload']['error'];
	$message = $upload_errors[$error];
}
}

?>
<html>
<head>
	<title>Upload</title>
</head>
<body>


	<?php if(!empty($message)) { echo "<p>{$message}</p>"; } ?>
	<form action="upload.php" enctype="multipart/form-data" method="POST">

	  <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
	  <input type="file" name="file_upload" value="<?php $upload ?>" />

	  <input type="submit" name="submit" value="Upload" />
	</form>

</body>
</html>


" THE SECRET OF CREATIVITY IS KNOWING HOW TO HIDE YOUR SOURCES "

Share this post


Link to post
Share on other sites
shadowk

Problema resolvido!

Com um pouquinho de paciencia e depois de almoçar sempre consegui  :cheesygrin:

Aqui está a solução:

<?php

$upload_errors = array(
  UPLOAD_ERR_OK 		=> "No errors.",
  UPLOAD_ERR_INI_SIZE  	=> "Larger than upload_max_filesize.",
  UPLOAD_ERR_FORM_SIZE 	=> "Larger than form MAX_FILE_SIZE.",
  UPLOAD_ERR_PARTIAL 	=> "Partial upload.",
  UPLOAD_ERR_NO_FILE 	=> "No file.",
  UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
  UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
  UPLOAD_ERR_EXTENSION 	=> "File upload stopped by extension."
);

if(isset($_POST['submit'])) {
$tmp_file = $_FILES['file_upload']['tmp_name'];
$target_file = basename($_FILES['file_upload']['name']);
$upload_dir = "uploads";

if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file)) {
	$message = "" .$upload_dir. "/" .$target_file. "";
} else {
	$error = $_FILES['file_upload']['error'];
	$message = $upload_errors[$error];
}
}

?>
<html>
<head>
	<title>Upload</title>
</head>
<body>


	<?php
        include("ligacao.php");

         if(!empty($message)) {

            //echo "<p>{$message}</p>";
            $query = "INSERT INTO img (
			img
		) VALUES (
			'{$message}'
		)";
            $criar = mysql_query($query);
            if($criar){
  	        echo "Imagem inserida";
        }else{
            echo "Erro" . mysql_error();
        }

          } ?>
	<form action="upload.php" enctype="multipart/form-data" method="POST">

	  <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
	  <input type="file" name="file_upload" value="" />

	  <input type="submit" name="submit" value="Upload" />
	</form>

</body>
</html>

Obrigado na mesma :thumbsup:


" THE SECRET OF CREATIVITY IS KNOWING HOW TO HIDE YOUR SOURCES "

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

By using this site you accept our Terms of Use and Privacy Policy. 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.