Page 1 of 1

Rename on upload

Posted: Tue Mar 15, 2011 5:38 am
by _zob_
Hello there! Im trying to rename an image file on upload when im editing a content through a backoffice.
The edit functionality works well, but i need to rename the image to the result of a random var.
As you can see in the example:

Code: Select all

<?php
	require_once("../classes/Queries.php");
	require_once("../classes/Uploads.php");
	require_once("../classes/XML.php");
	
	$nodes = array();
	
	function detectaIE()
	{
	    if (isset($_SERVER['HTTP_USER_AGENT']) && 
	    (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false))
	        return true;
	    else
	        return false;
	}
	
        $obj = new stdClass();
	$obj->tabelas = array("tab_texturas");
	$obj->campos = array(array("nome", "'".$_POST['textura']."'"));
	$obj->registo = array("id", $_POST['id']);
	
	$obj_imagem = new stdClass();
	$obj_imagem->tabelas = array("tab_texturas");
	$obj_imagem->registo = array("id", $_POST['id']);
	
	$actualiza = new Queries();
	
	$update = $actualiza->actualizaRegisto($obj);
	
	if ($update->estado == 1){
		
		$sql = "UPDATE tab_texturas SET nome = '" . $_POST['textura']."' WHERE id = " . $_POST['id'];
		if ($query = mysql_db_query("margon", $sql)){
		
			$imagem = new Uploads();
			
			if (detectaIE()){
				$tipo = "image/pjpeg";
			} else {
				$tipo = "image/jpeg";
			}
			
			$uploads = $imagem->upload('imagem', '../../imagens/texturas', $tipo);
			
			$random=rand(0,9999);
    		        $newpicname=$random;
			$uploadfile = $newpicname;
			$pasta = "imagens/texturas";
			$ficheiro = $pasta . "/" . $uploadfile . ".jpg";	
			
			if ($uploads->estado == 1){
				
				$obj_imagem->campos = array(array("imagem", "'" . substr($uploads->upload, 6) . "'"));
				
				$update = $actualiza->actualizaRegisto($obj_imagem);
			
			if ($update->estado != 1){
				print("Ocorreu o erroC: ".$update->numErro. " - ".$update->erro."<br>".$update->sql);
				}
			} else if ($uploads->estado == 0){
				print("Ocorreu o erroB: ".$uploads->erro);
			} else if ($uploads->estado == 2){
				print("Ocorreu o erroA: ".$uploads->erroFicheiro . "");
			} else {
				print("Ocorreu o erroX: ".$uploads->estado);
			}
			
			header("Location:../index2.php?cmd=prod&ver=textedit&id=".$_POST['id']);
		
		} else {
			print ("ERRO FATAL: " . mysql_error() . "<br />" . $sql);
		}
		
		
	} else {
		print("Ocorreu o erroX: ".$update->numErro. " - ".$update->erro."<br>".$update->sql);
	}
?>
When im able to rename the file, i lose the capability of uploading it.
I believe that im near the solution and it might be a problem of bad syntax construction near:

Code: Select all

$obj_imagem->campos = array(array("imagem", "'" . substr($uploads->upload, 6) . "'"));
This is where im trying to add the random renaming functionality.
Im quite new to php so please forgive any stupidity from my behalf!

Thank you in advance!

Re: Rename on upload

Posted: Tue Mar 15, 2011 11:29 am
by social_experiment

Code: Select all

<?php
if ( $_FILES['logoimg']['name'] != '' ) {
$number = '1234567890';
$random = mt_rand(10000, $number);
					
$sub_fileName = $random;
$validExtension = '.jpg';
$new_fileName = $sub_fileName.$validExtension;					
					
$source = $_FILES['logoimg']['tmp_name'];
$target = "../path/to/file/$new_fileName";					
$move = @move_uploaded_file( $source, $target );				
} 
?>
This is how i'd go about creating a random name for a file. You don't show much of the inner workings of the method (upload()) so it could be a few things. Are you getting any syntax errors / warnings?

Re: Rename on upload

Posted: Tue Mar 15, 2011 11:53 am
by _zob_
Hello and thanks for replying!
Well, i don't get any warnings/errors on syntax.
The upload works well, the renaming also works well, both options at the same time don't!
I use the same random method when inserting content to the DB successfully, but when editing content both options don't work.
I need to use numbers on file name because otherwise, at the frontOffice, fancy-box cant open the image(when letters are in the filename (don't ask me why!))

Here is the upload class (working):

Code: Select all

<?php

class Uploads
{
	
	public function upload($ficheiro, $pasta, $tipo)
	{
		$resultado = new stdClass();
		
		if (($_FILES[$ficheiro]['name'] != "none") && ($_FILES[$ficheiro]['name'] != "")) {
			if ($_FILES[$ficheiro]['type'] != $tipo) {
				$resultado->estado = 2;
				$resultado->erroFicheiro = $_FILES[$ficheiro]['error'] . " | ". $_FILES[$ficheiro]['type'];
			} else {
				if (move_uploaded_file($_FILES[$ficheiro]['tmp_name'], $pasta."/".$_FILES[$ficheiro]['name'])){
					$resultado->estado = 1;
					$resultado->upload = $pasta."/".$_FILES[$ficheiro]['name'];
				} else {
					$resultado->estado = 0;
					$resultado->erro = "ERRO FATAL: ".$_FILES[$ficheiro]['error'];
				}
			}
		} else {
			$resultado->estado = 3;
		}
		
		return $resultado;
	}
	
}
?>
I did not wanted to mess with this class because it is used for other uploads that doesn't need renaming on upload, thats why im trying to add the random naming specifically for the situation described initially.

I think the solution is near, maybe messing a bit with this line:

Code: Select all

$obj_imagem->campos = array(array("imagem", "'" . substr($uploads->upload, 6) . "'"));
Any ideas? Thank you!